Class: Lockbox::Encryptor
- Inherits:
-
Object
- Object
- Lockbox::Encryptor
- Defined in:
- lib/lockbox/encryptor.rb
Instance Method Summary collapse
- #decrypt(ciphertext, **options) ⇒ Object
- #decrypt_io(io, **options) ⇒ Object
- #decrypt_str(ciphertext, **options) ⇒ Object
- #encrypt(message, **options) ⇒ Object
- #encrypt_io(io, **options) ⇒ Object
-
#initialize(**options) ⇒ Encryptor
constructor
A new instance of Encryptor.
Constructor Details
#initialize(**options) ⇒ Encryptor
Returns a new instance of Encryptor.
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/lockbox/encryptor.rb', line 3 def initialize(**) = Lockbox..merge() @encode = .delete(:encode) # option may be renamed to binary: true # warn "[lockbox] Lockbox 1.0 will default to encode: true. Pass encode: false to keep the current behavior." if @encode.nil? previous_versions = .delete(:previous_versions) @boxes = [Box.new(**)] + Array(previous_versions).reject { |v| v.key?(:master_key) }.map { |v| Box.new(key: [:key], **v) } end |
Instance Method Details
#decrypt(ciphertext, **options) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/lockbox/encryptor.rb', line 22 def decrypt(ciphertext, **) ciphertext = Base64.decode64(ciphertext) if @encode ciphertext = check_string(ciphertext) # ensure binary if ciphertext.encoding != Encoding::BINARY # dup to prevent mutation ciphertext = ciphertext.dup.force_encoding(Encoding::BINARY) end @boxes.each_with_index do |box, i| begin return box.decrypt(ciphertext, **) rescue => e # returning DecryptionError instead of PaddingError # is for end-user convenience, not for security error_classes = [DecryptionError, PaddingError] error_classes << RbNaCl::LengthError if defined?(RbNaCl::LengthError) error_classes << RbNaCl::CryptoError if defined?(RbNaCl::CryptoError) if error_classes.any? { |ec| e.is_a?(ec) } raise DecryptionError, "Decryption failed" if i == @boxes.size - 1 else raise e end end end end |
#decrypt_io(io, **options) ⇒ Object
56 57 58 59 60 |
# File 'lib/lockbox/encryptor.rb', line 56 def decrypt_io(io, **) new_io = Lockbox::IO.new(decrypt(io.read, **)) (io, new_io) new_io end |
#decrypt_str(ciphertext, **options) ⇒ Object
62 63 64 65 |
# File 'lib/lockbox/encryptor.rb', line 62 def decrypt_str(ciphertext, **) = decrypt(ciphertext, **) .force_encoding(Encoding::UTF_8) end |
#encrypt(message, **options) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/lockbox/encryptor.rb', line 15 def encrypt(, **) = check_string() ciphertext = @boxes.first.encrypt(, **) ciphertext = Base64.strict_encode64(ciphertext) if @encode ciphertext end |