Method: Lockbox::Box#encrypt

Defined in:
lib/lockbox/box.rb

#encrypt(message, associated_data: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lockbox/box.rb', line 37

def encrypt(message, associated_data: nil)
  message = Lockbox.pad(message, size: @padding) if @padding
  case @algorithm
  when "hybrid"
    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
    nonce = generate_nonce(@box)
    ciphertext = @box.encrypt(nonce, message)
  else
    nonce = generate_nonce(@box)
    ciphertext = @box.encrypt(nonce, message, associated_data)
  end
  nonce + ciphertext
end