Module: Ronin::Support::Crypto::Mixin

Included in:
Mixin
Defined in:
lib/ronin/support/crypto/mixin.rb

Overview

Provides helper methods for cryptographic functions.

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#crypto_aes128_cipher(**kwargs) ⇒ Cipher::AES Also known as: aes128_cipher

Creates a new AES-128 cipher.

Examples:

Crypto.aes128_cipher(direction: :encrypt, password: 's3cr3t')
# => #<Ronin::Support::Crypto::Cipher::AES128:0x00007f8bde789648 @key_size=128, @mode=:cbc>

Parameters:

Options Hash (**kwargs):

  • :mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :md5

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

See Also:

Since:

  • 1.0.0



341
342
343
# File 'lib/ronin/support/crypto/mixin.rb', line 341

def crypto_aes128_cipher(**kwargs)
  Crypto.aes128_cipher(**kwargs)
end

#crypto_aes128_decrypt(data, **kwargs) ⇒ String Also known as: aes128_decrypt

Decrypts data using AES-128.

Parameters:

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :md5

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



423
424
425
# File 'lib/ronin/support/crypto/mixin.rb', line 423

def crypto_aes128_decrypt(data,**kwargs)
  Crypto.aes128_decrypt(data,**kwargs)
end

#crypto_aes128_encrypt(data, **kwargs) ⇒ String Also known as: aes128_encrypt

Encrypts data using AES-128.

Parameters:

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :md5

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



382
383
384
# File 'lib/ronin/support/crypto/mixin.rb', line 382

def crypto_aes128_encrypt(data,**kwargs)
  Crypto.aes128_encrypt(data,**kwargs)
end

#crypto_aes256_cipher(**kwargs) ⇒ Cipher::AES Also known as: aes256_cipher

Creates a new AES-256 cipher.

Examples:

Crypto.aes256(direction: :encrypt, password: 's3cr3t')
# => #<Ronin::Support::Crypto::Cipher::AES256:0x00007f8bde789648 @key_size=256, @mode=:cbc>

Parameters:

Options Hash (**kwargs):

  • :mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

See Also:

Since:

  • 1.0.0



462
463
464
# File 'lib/ronin/support/crypto/mixin.rb', line 462

def crypto_aes256_cipher(**kwargs)
  Crypto.aes256_cipher(**kwargs)
end

#crypto_aes256_decrypt(data, **kwargs) ⇒ String Also known as: aes256_decrypt

Decrypts data using AES-256.

Parameters:

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sh256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



544
545
546
# File 'lib/ronin/support/crypto/mixin.rb', line 544

def crypto_aes256_decrypt(data,**kwargs)
  Crypto.aes256_decrypt(data,**kwargs)
end

#crypto_aes256_encrypt(data, **kwargs) ⇒ String Also known as: aes256_encrypt

Encrypts data using AES-256.

Parameters:

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



503
504
505
# File 'lib/ronin/support/crypto/mixin.rb', line 503

def crypto_aes256_encrypt(data,**kwargs)
  Crypto.aes256_encrypt(data,**kwargs)
end

#crypto_aes_cipher(**kwargs) ⇒ Cipher::AES Also known as: aes_cipher

Creates a new AES cipher.

Examples:

crypto_aes_cipher(direction: :encrypt, password: 's3cr3t')
# => #<Ronin::Support::Crypto::Cipher::AES:0x00007f2b84dfa6b8 @key_size=256, @mode=:cbc>

Parameters:

Options Hash (**kwargs):

  • :key_size (Integer)

    The desired key size in bits.

  • :mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

See Also:

Since:

  • 1.0.0



214
215
216
# File 'lib/ronin/support/crypto/mixin.rb', line 214

def crypto_aes_cipher(**kwargs)
  Crypto.aes_cipher(**kwargs)
end

#crypto_aes_decrypt(data, **kwargs) ⇒ String Also known as: aes_decrypt

Decrypts data using AES.

Parameters:

Options Hash (**kwargs):

  • :key_size (Integer)

    The desired key size in bits.

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



302
303
304
# File 'lib/ronin/support/crypto/mixin.rb', line 302

def crypto_aes_decrypt(data,**kwargs)
  Crypto.aes_decrypt(data,**kwargs)
end

#crypto_aes_encrypt(data, **kwargs) ⇒ String Also known as: aes_encrypt

Encrypts data using AES.

Parameters:

Options Hash (**kwargs):

  • :key_size (Integer)

    The desired key size in bits.

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



258
259
260
# File 'lib/ronin/support/crypto/mixin.rb', line 258

def crypto_aes_encrypt(data,**kwargs)
  Crypto.aes_encrypt(data,**kwargs)
end

#crypto_cipher(name, **kwargs) ⇒ OpenSSL::Cipher Also known as: cipher

Creates a cipher.

Examples:

crypto_cipher('aes-128-cbc', mode: :encrypt, key 'secret'.md5)
# => #<OpenSSL::Cipher:0x0000000170d108>

Parameters:

  • name (String)

    The cipher name.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for Cipher#initialize.

Options Hash (**kwargs):

  • :direction (:encrypt, :decrypt)

    Specifies whether to encrypt or decrypt data.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (OpenSSL::Cipher)

    The newly created cipher.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



120
121
122
# File 'lib/ronin/support/crypto/mixin.rb', line 120

def crypto_cipher(name,**kwargs)
  Crypto.cipher(name,**kwargs)
end

#crypto_decrypt(data, cipher:, **kwargs) ⇒ String Also known as: decrypt

Decrypts data using the cipher.

Parameters:

  • data (#to_s)

    The data to decrypt.

  • cipher (String)

    The cipher name (ex: "aes-256-cbc").

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #cipher.

Returns:

  • (String)

    The decrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



172
173
174
# File 'lib/ronin/support/crypto/mixin.rb', line 172

def crypto_decrypt(data, cipher: ,**kwargs)
  Crypto.decrypt(data, cipher: cipher, **kwargs)
end

#crypto_digest(name) ⇒ OpenSSL::Digest Also known as: digest

Looks up a digest.

Examples:

crypto_digest(:ripemd160)
# => OpenSSL::Digest::RIPEMD160

Parameters:

  • name (String, Symbol)

    The name of the digest.

Returns:

  • (OpenSSL::Digest)

    The OpenSSL Digest class.

See Also:

Since:

  • 1.0.0



47
48
49
# File 'lib/ronin/support/crypto/mixin.rb', line 47

def crypto_digest(name)
  Crypto.digest(name)
end

#crypto_encrypt(data, cipher:, **kwargs) ⇒ String Also known as: encrypt

Encrypts data using the cipher.

Parameters:

  • data (#to_s)

    The data to encrypt.

  • cipher (String)

    The cipher name (ex: "aes-256-cbc").

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

See Also:

Since:

  • 1.0.0



146
147
148
# File 'lib/ronin/support/crypto/mixin.rb', line 146

def crypto_encrypt(data, cipher: ,**kwargs)
  Crypto.encrypt(data, cipher: cipher, **kwargs)
end

#crypto_hmac(data = nil, key:, digest: :sha1) ⇒ String Also known as: hmac

Creates a new HMAC.

Examples:

crypto_hmac('secret')

Parameters:

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

    The optional data to sign.

  • key (String)

    The secret key for the HMAC.

  • digest (Symbol) (defaults to: :sha1)

    The digest algorithm for the HMAC.

Returns:

  • (String)

    The hex-encoded HMAC for the String.

See Also:

Since:

  • 1.0.0



75
76
77
# File 'lib/ronin/support/crypto/mixin.rb', line 75

def crypto_hmac(data=nil, key: , digest: :sha1)
  Crypto.hmac(data, key: key, digest: digest)
end

#crypto_rsa_decrypt(data, **kwargs) ⇒ String Also known as: rsa_decrypt

Decrypts the given data using the given RSA key.

Optional padding mode. nil and false will disable padding.

Parameters:

Options Hash (**kwargs):

  • :key (String, nil)

    The PEM or DER encoded RSA key string.

  • :key_file (String, nil)

    The path to the PEM or DER encoded RSA key file.

  • :key_password (String, nil)

    The optional password to decrypt the encrypted RSA key.

  • :padding (:pkcs1_oaep, :pkcs1, :sslv23, nil, false) — default: :pkcs1

Returns:

  • (String)

    The decrypted data.

Raises:

  • (ArgumentError)

    Either the key: or key_file: keyword argument must be given.

Since:

  • 1.0.0



612
613
614
# File 'lib/ronin/support/crypto/mixin.rb', line 612

def crypto_rsa_decrypt(data,**kwargs)
  Crypto.rsa_decrypt(data,**kwargs)
end

#crypto_rsa_encrypt(data, **kwargs) ⇒ String Also known as: rsa_encrypt

Encrypts the given data using the given RSA key.

Optional padding mode. nil and false will disable padding.

Parameters:

Options Hash (**kwargs):

  • :key (String, nil)

    The PEM or DER encoded RSA key string.

  • :key_file (String, nil)

    The path to the PEM or DER encoded RSA key file.

  • :key_password (String, nil)

    The optional password to decrypt the encrypted RSA key.

  • :padding (:pkcs1_oaep, :pkcs1, :sslv23, nil, false) — default: :pkcs1

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the key: or key_file: keyword argument must be given.

Since:

  • 1.0.0



578
579
580
# File 'lib/ronin/support/crypto/mixin.rb', line 578

def crypto_rsa_encrypt(data,**kwargs)
  Crypto.rsa_encrypt(data,**kwargs)
end