lib/lockbox/box.rb in lockbox-0.4.9 vs lib/lockbox/box.rb in lockbox-0.5.0
- old
+ new
@@ -1,20 +1,19 @@
module Lockbox
class Box
def initialize(key: nil, algorithm: nil, encryption_key: nil, decryption_key: nil, padding: false)
- raise ArgumentError, "Cannot pass both key and public/private key" if key && (encryption_key || decryption_key)
+ raise ArgumentError, "Cannot pass both key and encryption/decryption key" if key && (encryption_key || decryption_key)
key = Lockbox::Utils.decode_key(key) if key
encryption_key = Lockbox::Utils.decode_key(encryption_key, size: 64) if encryption_key
decryption_key = Lockbox::Utils.decode_key(decryption_key, size: 64) if decryption_key
algorithm ||= "aes-gcm"
case algorithm
when "aes-gcm"
raise ArgumentError, "Missing key" unless key
- require "lockbox/aes_gcm"
@box = AES_GCM.new(key)
when "xchacha20"
raise ArgumentError, "Missing key" unless key
require "rbnacl"
@box = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(key)
@@ -37,11 +36,11 @@
def encrypt(message, associated_data: nil)
message = Lockbox.pad(message, size: @padding) if @padding
case @algorithm
when "hybrid"
- raise ArgumentError, "No public key set" unless @encryption_box
+ raise ArgumentError, "No encryption key set" unless defined?(@encryption_box)
raise ArgumentError, "Associated data not supported with this algorithm" if associated_data
nonce = generate_nonce(@encryption_box)
ciphertext = @encryption_box.encrypt(nonce, message)
when "xsalsa20"
raise ArgumentError, "Associated data not supported with this algorithm" if associated_data
@@ -56,10 +55,10 @@
def decrypt(ciphertext, associated_data: nil)
message =
case @algorithm
when "hybrid"
- raise ArgumentError, "No private key set" unless @decryption_box
+ raise ArgumentError, "No decryption key set" unless defined?(@decryption_box)
raise ArgumentError, "Associated data not supported with this algorithm" if associated_data
nonce, ciphertext = extract_nonce(@decryption_box, ciphertext)
@decryption_box.decrypt(nonce, ciphertext)
when "xsalsa20"
raise ArgumentError, "Associated data not supported with this algorithm" if associated_data