Sha256: 89e9b16c484aa981af9ccb9cccfd9504359e9700481dd6eddaa50ef4ed8a5554
Contents?: true
Size: 1.32 KB
Versions: 1
Compression:
Stored size: 1.32 KB
Contents
module Keyring require "openssl" require "base64" require "digest/sha1" require "keyring/key" require "keyring/encryptor/aes" UnknownKey = Class.new(StandardError) InvalidSecret = Class.new(StandardError) EmptyKeyring = Class.new(StandardError) class Base def initialize(keyring, encryptor) @encryptor = encryptor @keyring = keyring.map do |id, value| Key.new(id, value, @encryptor.key_size) end end def current_key @keyring.max_by(&:id) end 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 def []=(id, value) @keyring << Key.new(id, value, @encryptor.key_size) end def clear @keyring.clear end def encrypt(message, keyring_id = nil) keyring_id ||= current_key&.id digest = Digest::SHA1.hexdigest(message) [ @encryptor.encrypt(self[keyring_id].value, message), keyring_id, digest ] end def decrypt(message, keyring_id) @encryptor.decrypt(self[keyring_id].value, message) end end def self.new(keyring, encryptor = Encryptor::AES::AES128CBC) Base.new(keyring, encryptor) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
attr_keyring-0.4.0 | lib/keyring.rb |