Sha256: 85e5687cbdb882cf90adb74a9071cf4016c8fced9ebb28baa3c9d8c848443bfa
Contents?: true
Size: 1.42 KB
Versions: 2
Compression:
Stored size: 1.42 KB
Contents
# frozen_string_literal: true 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) InvalidAuthentication = 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, key) @keyring << Key.new(id, key, @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) key = self[keyring_id] [ @encryptor.encrypt(key, message), keyring_id, digest ] end def decrypt(message, keyring_id) key = self[keyring_id] @encryptor.decrypt(key, message) end end def self.new(keyring, encryptor = Encryptor::AES::AES128CBC) Base.new(keyring, encryptor) end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
attr_keyring-0.5.4 | lib/keyring.rb |
attr_keyring-0.5.3 | lib/keyring.rb |