module OffsitePayments::Integrations::SagePayForm::Encryption

Public Instance Methods

sage_decrypt(ciphertext, key) click to toggle source
# File lib/offsite_payments/integrations/sage_pay_form.rb, line 35
def sage_decrypt(ciphertext, key)
  ciphertext = ciphertext[1..-1] # remove @ symbol at the beginning of a string
  cipher(:decrypt, key, ciphertext)
rescue OpenSSL::Cipher::CipherError => e
  return '' if e.message == 'wrong final block length'
  raise
end
sage_encrypt(plaintext, key) click to toggle source
# File lib/offsite_payments/integrations/sage_pay_form.rb, line 30
def sage_encrypt(plaintext, key)
  encrypted = cipher(:encrypt, key, plaintext)
  "@#{encrypted.upcase}"
end
sage_encrypt_salt(min, max) click to toggle source
# File lib/offsite_payments/integrations/sage_pay_form.rb, line 43
def sage_encrypt_salt(min, max)
  length = rand(max - min + 1) + min
  SecureRandom.base64(length + 4)[0, length]
end

Private Instance Methods

cipher(action, key, payload) click to toggle source
# File lib/offsite_payments/integrations/sage_pay_form.rb, line 50
def cipher(action, key, payload)
  if action == :decrypt
    payload = [payload].pack('H*')
  end

  cipher = OpenSSL::Cipher::AES128.new(:CBC)
  cipher.public_send(action)
  cipher.key = key
  cipher.iv = key
  result = cipher.update(payload) + cipher.final

  if action == :encrypt
    result = result.unpack('H*')[0]
  end

  result
end