Sha256: 6102748c93e0bca4f9953dc8b09a6457d076ea1a27242165eb0b883a1e69209f

Contents?: true

Size: 1.97 KB

Versions: 43

Compression:

Stored size: 1.97 KB

Contents

module Zip
  module TraditionalEncryption
    def initialize(password)
      @password = password
      reset_keys!
    end

    def header_bytesize
      12
    end

    def gp_flags
      0x0001 | 0x0008
    end

    protected

    def reset_keys!
      @key0 = 0x12345678
      @key1 = 0x23456789
      @key2 = 0x34567890
      @password.each_byte do |byte|
        update_keys(byte.chr)
      end
    end

    def update_keys(num)
      @key0 = ~Zlib.crc32(num, ~@key0)
      @key1 = ((@key1 + (@key0 & 0xff)) * 134_775_813 + 1) & 0xffffffff
      @key2 = ~Zlib.crc32((@key1 >> 24).chr, ~@key2)
    end

    def decrypt_byte
      temp = (@key2 & 0xffff) | 2
      ((temp * (temp ^ 1)) >> 8) & 0xff
    end
  end

  class TraditionalEncrypter < Encrypter
    include TraditionalEncryption

    def header(mtime)
      [].tap do |header|
        (header_bytesize - 2).times do
          header << Random.rand(0..255)
        end
        header << (mtime.to_binary_dos_time & 0xff)
        header << (mtime.to_binary_dos_time >> 8)
      end.map { |x| encode x }.pack('C*')
    end

    def encrypt(data)
      data.unpack('C*').map { |x| encode x }.pack('C*')
    end

    def data_descriptor(crc32, compressed_size, uncomprssed_size)
      [0x08074b50, crc32, compressed_size, uncomprssed_size].pack('VVVV')
    end

    def reset!
      reset_keys!
    end

    private

    def encode(num)
      t = decrypt_byte
      update_keys(num.chr)
      t ^ num
    end
  end

  class TraditionalDecrypter < Decrypter
    include TraditionalEncryption

    def decrypt(data)
      data.unpack('C*').map { |x| decode x }.pack('C*')
    end

    def reset!(header)
      reset_keys!
      header.each_byte do |x|
        decode x
      end
    end

    private

    def decode(num)
      num ^= decrypt_byte
      update_keys(num.chr)
      num
    end
  end
end

# Copyright (C) 2002, 2003 Thomas Sondergaard
# rubyzip is free software; you can redistribute it and/or
# modify it under the terms of the ruby license.

Version data entries

43 entries across 33 versions & 8 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
rubyzip-2.4.rc1 lib/zip/crypto/traditional_encryption.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
lg_pod_plugin-1.0.8 lib/zip/crypto/traditional_encryption.rb
lg_pod_plugin-1.0.7 lib/zip/crypto/traditional_encryption.rb
lg_pod_plugin-1.0.6 lib/zip/crypto/traditional_encryption.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/rubyzip-2.3.0/lib/zip/crypto/traditional_encryption.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/rubyzip-2.3.2/lib/zip/crypto/traditional_encryption.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/rubyzip-2.3.0/lib/zip/crypto/traditional_encryption.rb