module SagepayProtocol3::Encryption

Public Instance Methods

cipher(operation, cipher, cipher_key) click to toggle source
# File lib/sagepay_protocol3/encryption.rb, line 8
def cipher(operation, cipher, cipher_key)
  lambda do |data, padding|
    c = OpenSSL::Cipher.new(cipher)
    c.send(operation)
    c.padding = padding
    c.key = cipher_key
    c.iv = cipher_key
    c.update(data) + c.final
  end
end
decrypt(cipher_key, data, cipher = "AES-128-CBC") click to toggle source
# File lib/sagepay_protocol3/encryption.rb, line 19
def decrypt(cipher_key, data, cipher = "AES-128-CBC")
  hex = data.gsub(/\A(@)/, '')
  input = [hex].pack('H*')
  output = cipher(:decrypt, cipher, cipher_key)[input, 0]
  sanitize_payload output
end
encrypt(cipher_key, data, cipher = "AES-128-CBC") click to toggle source
# File lib/sagepay_protocol3/encryption.rb, line 26
def encrypt(cipher_key, data, cipher = "AES-128-CBC")
  _encrypted = cipher(:encrypt, cipher, cipher_key)[data, 1]
  "@#{_encrypted.unpack('H*').first.upcase}"
end
sanitize_payload(string) click to toggle source
# File lib/sagepay_protocol3/encryption.rb, line 31
def sanitize_payload(string)
  string.gsub(/\005/, '').gsub("\r", '').gsub("\n", '')
end
to_h(crypt_string) click to toggle source
# File lib/sagepay_protocol3/encryption.rb, line 35
def to_h(crypt_string)
  Hash[*crypt_string.split(/[=&]/).reject(&:blank?)]
end