Class: Cryptor::SymmetricEncryption::SecretKey
- Inherits:
-
Object
- Object
- Cryptor::SymmetricEncryption::SecretKey
- Defined in:
- lib/cryptor/symmetric_encryption/secret_key.rb
Overview
Secret key used to encrypt plaintexts
Instance Attribute Summary collapse
-
#cipher ⇒ Object
readonly
Returns the value of attribute cipher.
Class Method Summary collapse
-
.random_key(cipher) ⇒ Cryptor::SecretKey
Generate a random secret key.
Instance Method Summary collapse
-
#decrypt(ciphertext) ⇒ String
Decrypt ciphertext using this key.
-
#encrypt(plaintext) ⇒ String
Encrypt a plaintext under this key.
-
#fingerprint ⇒ String
Fingerprint of this key’s secret URI.
-
#initialize(uri_string) ⇒ Cryptor::SecretKey
constructor
Create a new SecretKey object from a URI.
-
#inspect ⇒ String
Inspect this key.
-
#to_secret_uri ⇒ String
Serialize SecretKey object to a URI.
Constructor Details
#initialize(uri_string) ⇒ Cryptor::SecretKey
Create a new SecretKey object from a URI
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cryptor/symmetric_encryption/secret_key.rb', line 37 def initialize(uri_string) uri = URI.parse(uri_string.to_s) fail ArgumentError, "invalid scheme: #{uri.scheme}" unless uri.scheme == 'secret.key' components = uri.path.match(/^\/([^;]+);(.+)$/) fail ArgumentError, "couldn't parse cipher name from secret URI" unless components @cipher = Cryptor::SymmetricEncryption::Cipher[components[1]] @secret_key = Cryptor::Encoding.decode(components[2]) end |
Instance Attribute Details
#cipher ⇒ Object (readonly)
Returns the value of attribute cipher.
8 9 10 |
# File 'lib/cryptor/symmetric_encryption/secret_key.rb', line 8 def cipher @cipher end |
Class Method Details
.random_key(cipher) ⇒ Cryptor::SecretKey
Generate a random secret key
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cryptor/symmetric_encryption/secret_key.rb', line 15 def self.random_key(cipher) case cipher when Cryptor::SymmetricEncryption::Cipher # we're good when Symbol cipher = Cryptor::SymmetricEncryption::Cipher[cipher] else fail ArgumentError, "invalid cipher: #{cipher}" end bytes = RbNaCl::Random.random_bytes(cipher.key_bytes) base64 = Cryptor::Encoding.encode(bytes) new "secret.key:///#{cipher.algorithm};#{base64}" end |
Instance Method Details
#decrypt(ciphertext) ⇒ String
Decrypt ciphertext using this key
77 78 79 |
# File 'lib/cryptor/symmetric_encryption/secret_key.rb', line 77 def decrypt(ciphertext) @cipher.decrypt(@secret_key, ciphertext) end |
#encrypt(plaintext) ⇒ String
Encrypt a plaintext under this key
68 69 70 |
# File 'lib/cryptor/symmetric_encryption/secret_key.rb', line 68 def encrypt(plaintext) @cipher.encrypt(@secret_key, plaintext) end |
#fingerprint ⇒ String
Fingerprint of this key’s secret URI
58 59 60 61 |
# File 'lib/cryptor/symmetric_encryption/secret_key.rb', line 58 def fingerprint digest = Digest::SHA256.digest(to_secret_uri) "ni:///sha-256;#{Cryptor::Encoding.encode(digest)}" end |
#inspect ⇒ String
Inspect this key
84 85 86 87 88 |
# File 'lib/cryptor/symmetric_encryption/secret_key.rb', line 84 def inspect "#<#{self.class}:0x#{object_id.to_s(16)} " \ "cipher=#{cipher.algorithm} " \ "fingerprint=#{fingerprint}>" end |