Class: Keyring::Base
- Inherits:
-
Object
- Object
- Keyring::Base
- Defined in:
- lib/keyring.rb
Instance Method Summary collapse
- #[](id) ⇒ Object
- #[]=(id, value) ⇒ Object
- #clear ⇒ Object
- #current_key ⇒ Object
- #decrypt(message, keyring_id) ⇒ Object
- #encrypt(message, keyring_id = nil) ⇒ Object
-
#initialize(keyring, encryptor) ⇒ Base
constructor
A new instance of Base.
Constructor Details
Instance Method Details
#[](id) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/keyring.rb', line 25 def [](id) raise EmptyKeyring, "keyring doesn't have any keys" if @keyring.empty? key = @keyring.find {|k| k.id == id.to_i } return key if key raise UnknownKey, "key=#{id} is not available on keyring" end |
#[]=(id, value) ⇒ Object
34 35 36 |
# File 'lib/keyring.rb', line 34 def []=(id, value) @keyring << Key.new(id, value, @encryptor.key_size) end |
#clear ⇒ Object
38 39 40 |
# File 'lib/keyring.rb', line 38 def clear @keyring.clear end |
#current_key ⇒ Object
21 22 23 |
# File 'lib/keyring.rb', line 21 def current_key @keyring.max_by(&:id) end |
#decrypt(message, keyring_id) ⇒ Object
53 54 55 |
# File 'lib/keyring.rb', line 53 def decrypt(, keyring_id) @encryptor.decrypt(self[keyring_id].value, ) end |
#encrypt(message, keyring_id = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/keyring.rb', line 42 def encrypt(, keyring_id = nil) keyring_id ||= current_key&.id digest = Digest::SHA1.hexdigest() [ @encryptor.encrypt(self[keyring_id].value, ), keyring_id, digest ] end |