Class: EvpBytesToKey::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/evp_bytes_to_key/key.rb

Overview

This class is used to generate a new encryption key (and iv, if applicable) from a password in a way that is compatible with how the openssl command line utility generates keys and ivs. It emulates the logic of EVP_KeyToBytes from OpenSSL but is not a direct drop-in replacement because it does not support options like the number of rounds to use or the message digest algorithm to use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password = nil, salt = nil, bits = nil, iv_length = nil) ⇒ EvpBytesToKey::Key

Generate a key from a given password. This key is identical to the key generated by EVP_KeyToBytes() in the openssl command-line utility.

Parameters:

  • password (String) (defaults to: nil)

    the password used for key generation

  • salt (String, nil) (defaults to: nil)

    the salt used for key generation

  • bits (Integer) (defaults to: nil)

    the bit length of the key, must be divisible by 8

  • iv_length (Integer) (defaults to: nil)

    the byte length of the IV



24
25
26
27
28
29
30
31
# File 'lib/evp_bytes_to_key/key.rb', line 24

def initialize(password = nil, salt = nil, bits = nil, iv_length = nil)
  @password = validate_password(password)
  @salt = validate_salt(salt)
  @bits = validate_bits(bits)
  @iv_length = validate_iv_length(iv_length)

  generate_key!
end

Instance Attribute Details

#ivString

Returns the key or iv value in the named format.

Returns:

  • (String)

    the key or iv value in the named format



12
13
14
# File 'lib/evp_bytes_to_key/key.rb', line 12

def iv
  @iv
end

#iv_hexString

Returns the key or iv value in the named format.

Returns:

  • (String)

    the key or iv value in the named format



12
13
14
# File 'lib/evp_bytes_to_key/key.rb', line 12

def iv_hex
  @iv_hex
end

#keyString

Returns the key or iv value in the named format.

Returns:

  • (String)

    the key or iv value in the named format



12
13
14
# File 'lib/evp_bytes_to_key/key.rb', line 12

def key
  @key
end

#key_hexString

Returns the key or iv value in the named format.

Returns:

  • (String)

    the key or iv value in the named format



12
13
14
# File 'lib/evp_bytes_to_key/key.rb', line 12

def key_hex
  @key_hex
end