class Manacle::Helper

Public Class Methods

encrypt(password_string) click to toggle source
# File lib/manacle/helpers.rb, line 3
def self.encrypt(password_string)
  require 'openssl'
  require 'base64'

  # this creates the key, and the cipher
  secret = OpenSSL::PKCS5.pbkdf2_hmac_sha1(ENV['CONSUMER_SECRET'], ENV['CONSUMER_KEY'], 1024, 128)
  cipher = OpenSSL::Cipher::Cipher.new('AES-128-CBC')
  cipher.encrypt
  iv = cipher.random_iv
  cipher.key = secret
  cipher.iv = iv

  # the following does the encryption
  encrypted_data = cipher.update(password_string)
  encrypted_data << cipher.final

  # the encrypted string & the IV (concatenated by --) is to be sent across to the ESB
  encrypted_password = [encrypted_data, iv].map {|v| Base64.strict_encode64(v)}.join("--")
  return encrypted_password
end