class ResponseEncryption::SymmetricEncrypter

Attributes

cipher[R]
encrypted_data[R]
iv[R]
key[R]

Public Class Methods

encoded_nonce() click to toggle source
# File lib/response_encryption/symmetric_encrypter.rb, line 32
def encoded_nonce
  Base64.encode64(ResponseEncryption.cipher.random_iv)
end
new(options={}) click to toggle source
# File lib/response_encryption/symmetric_encrypter.rb, line 5
def initialize(options={})
  validate(options)
  @cipher = ResponseEncryption.cipher
  @iv = Base64.decode64(options[:encoded_iv])
  @key = Base64.decode64(options[:encoded_key])
end

Public Instance Methods

encrypt(data, encode_data = true) click to toggle source

@param data [ Object ] which respond to to_s @param encode_data [ Boolean ] @return [ String ] with the encrypted and encoded information

# File lib/response_encryption/symmetric_encrypter.rb, line 15
def encrypt(data, encode_data = true)
  return data if data.blank?
  cipher.encrypt
  cipher.key = key
  # This is the initial vector that we will use as nonce code.
  # This nonce is going in the headers as a 'Replay-Nonce'
  cipher.iv = iv
  encrypted = cipher.update(data.to_s) + cipher.final
  @encrypted_data = encode_data ? Base64.encode64(encrypted) : encrypted
end
validate(options) click to toggle source
# File lib/response_encryption/symmetric_encrypter.rb, line 26
def validate(options)
  errors.add(:param_missing, 'You must to set the encoded_iv (nonce) option') if options[:encoded_iv].blank?
  errors.add(:param_missing, 'You must to set the encoded_key (symmetric-key) option') if options[:encoded_key].blank?
end