Skip to content

Instantly share code, notes, and snippets.

@mrpunkin
Created August 22, 2014 21:10
Show Gist options
  • Select an option

  • Save mrpunkin/c0dc267e0ac55db4a906 to your computer and use it in GitHub Desktop.

Select an option

Save mrpunkin/c0dc267e0ac55db4a906 to your computer and use it in GitHub Desktop.
Encryption / decryption of token. Works fine in console, fails when run through passenger and stored in DB.
def encrypt_token
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.encrypt
aes.key = Rails.application.config.shopify.secret
aes.iv = iv = aes.random_iv
encrypted = aes.update(self[:token])
encrypted << aes.final
encoded = Base64.strict_encode64(iv + encrypted)
self.send(:token=, encoded)
end
def decrypt_token
t = self[:token]
decoded = Base64.strict_decode64(t)
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.decrypt
aes.key = Rails.application.config.shopify.secret
aes.iv = decoded.slice!(0,16)
decrypted = aes.update(decoded)
decrypted << aes.final
decrypted
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment