Module: PreciousCargo::Secret

Defined in:
lib/precious_cargo/secret.rb

Overview

Public: A collection of methods to encrypt and decrypt the secret key used to encrypt the payload’s data.

Class Method Summary collapse

Class Method Details

.decrypt!(options = {}) ⇒ Object

Public: Decrypt the supplied Base64 encoded secret string using an RSA key pair object.

options - Hash of values used to decrypt the secret.

:encrypted_secret - A Base64 encoded, RSA encrypted secret string.
:keypair          - The RSA key pair object used to decrypt the secret.

Returns the decrypted secret.



29
30
31
32
33
# File 'lib/precious_cargo/secret.rb', line 29

def decrypt!(options = {})
  encrypted_secret = Base64.decode64(options[:encrypted_secret])
  keypair = options[:keypair]
  keypair.private_decrypt(encrypted_secret)
end

.encrypt!(options = {}) ⇒ Object

Public: Encrypt the supplied secret string using an RSA public key object. If a secret is not supplied, then a random secret is generated. It is generally better to randomly generate a secret every time you encrypt your precious cargo.

options - Hash of values used to encrypt the secret.

:secret     - A secret string. If a secret string is not passed in, then one is randomly generated.
:public_key - The RSA public key object used to encrypt the secret.

Returns the RSA encrypted secret as a Base64 encoded string.



16
17
18
19
20
# File 'lib/precious_cargo/secret.rb', line 16

def encrypt!(options = {})
  secret = options[:secret]
  public_key = options[:public_key]
  Base64.encode64(public_key.public_encrypt(secret))
end

.randomObject

Public: Generates a random 32 character string.

Returns random 32 character string.



38
39
40
# File 'lib/precious_cargo/secret.rb', line 38

def random
  Array.new(32){rand(36).to_s(36)}.join
end