Class: Omnizip::Password::EncryptionStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/password/encryption_strategy.rb

Overview

Base class for encryption strategies Defines the interface for encrypting/decrypting archive entries

Direct Known Subclasses

WinzipAesStrategy, ZipCryptoStrategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password) ⇒ EncryptionStrategy

Initialize encryption strategy

Parameters:

  • password (String)

    Password to use



12
13
14
15
# File 'lib/omnizip/password/encryption_strategy.rb', line 12

def initialize(password)
  @password = password
  validate_password
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/omnizip/password/encryption_strategy.rb', line 8

def password
  @password
end

Instance Method Details

#compression_methodInteger

Get encryption method ID for ZIP format

Returns:

  • (Integer)

    Compression method ID

Raises:

  • (NotImplementedError)

    Subclasses must implement



36
37
38
39
# File 'lib/omnizip/password/encryption_strategy.rb', line 36

def compression_method
  raise NotImplementedError,
        "#{self.class} must implement #compression_method"
end

#decrypt(data) ⇒ String

Decrypt data

Parameters:

  • data (String)

    Data to decrypt

Returns:

  • (String)

    Decrypted data

Raises:

  • (NotImplementedError)

    Subclasses must implement



29
30
31
# File 'lib/omnizip/password/encryption_strategy.rb', line 29

def decrypt(data)
  raise NotImplementedError, "#{self.class} must implement #decrypt"
end

#encrypt(data) ⇒ String

Encrypt data

Parameters:

  • data (String)

    Data to encrypt

Returns:

  • (String)

    Encrypted data

Raises:

  • (NotImplementedError)

    Subclasses must implement



21
22
23
# File 'lib/omnizip/password/encryption_strategy.rb', line 21

def encrypt(data)
  raise NotImplementedError, "#{self.class} must implement #encrypt"
end

#encryption_flagsInteger

Get encryption flags for ZIP header

Returns:

  • (Integer)

    Encryption flags



49
50
51
# File 'lib/omnizip/password/encryption_strategy.rb', line 49

def encryption_flags
  0x0001 # Bit 0: encrypted
end

#extra_field_dataString

Get extra field data for ZIP header

Returns:

  • (String)

    Extra field data



43
44
45
# File 'lib/omnizip/password/encryption_strategy.rb', line 43

def extra_field_data
  ""
end

#method_nameSymbol

Get encryption method name

Returns:

  • (Symbol)

    Method name



62
63
64
65
66
67
68
69
# File 'lib/omnizip/password/encryption_strategy.rb', line 62

def method_name
  self.class.name.split("::").last
    .gsub(/Strategy$/, "")
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .downcase
    .to_sym
end

#supports?(_data) ⇒ Boolean

Check if this strategy supports the given data

Parameters:

  • data (String)

    Data to check

Returns:

  • (Boolean)

    True if supported



56
57
58
# File 'lib/omnizip/password/encryption_strategy.rb', line 56

def supports?(_data)
  true
end