Class: Archive::Zip::Codec::TraditionalEncryption::Writer

Inherits:
Base
  • Object
show all
Defined in:
lib/archive/zip/codec/traditional_encryption/writer.rb

Overview

Archive::Zip::Codec::TraditionalEncryption#compressor method.

Instance Method Summary collapse

Methods inherited from Base

#seek

Constructor Details

#initialize(delegate, password, mtime, autoclose: true) ⇒ Writer

Returns a new instance of Writer.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/archive/zip/codec/traditional_encryption/writer.rb', line 16

def initialize(delegate, password, mtime, autoclose: true)
  super

  # A 12 byte header to protect the encrypted file data from attack.  The
  # first 10 bytes are random, and the last 2 bytes are the low order word
  # in little endian byte order of the last modified time of the entry in
  # DOS format.
  @header =
    (10.times.map { |_| rand(256) } + [DOSTime.new(@mtime).to_i].pack('V')[0, 2].bytes)
    .map do |byte|
      crypt_char = (byte ^ decrypt_byte).chr
      update_keys(byte.chr)
      crypt_char
    end
    .join
end

Instance Method Details

#write(buffer, length: buffer.bytesize) ⇒ Object

Encrypts and writes string to the delegate IO object. Returns the number of bytes of string written.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/archive/zip/codec/traditional_encryption/writer.rb', line 35

def write(buffer, length: buffer.bytesize)
  result = write_header
  return result if Symbol === result

  buffer = buffer[0, length] if length < buffer.bytesize
  buffer.to_enum(:each_byte).each_with_index do |byte, idx|
    result = super((byte ^ decrypt_byte).chr)
    if Symbol === result
      return idx if idx > 0
      return result
    end
    update_keys(byte.chr)
    @bytes_processed += 1
  end

  buffer.bytesize
end