Created
October 17, 2014 17:32
-
-
Save edeustace/e42db1ee594f0ea44187 to your computer and use it in GitHub Desktop.
An example of aes encryption in ruby for CoreSpring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'openssl' | |
| ### | |
| # An implementation of the AES encryption spec | |
| # see: http://www.corespring.org/developer/home#encrypting-player-options | |
| ### | |
| class EncryptionHelper | |
| Type = "AES-128-CBC" | |
| def self.encrypt(data, client_secret) | |
| aes = OpenSSL::Cipher::Cipher.new(Type) | |
| iv = aes.random_iv | |
| iv_hexed = iv.unpack('H*')[0] | |
| key = self.digest_key(client_secret) | |
| aes.encrypt | |
| aes.key = key | |
| aes.iv = iv | |
| cipher = aes.update(data) | |
| cipher << aes.final | |
| encrypted_hexed = cipher.unpack('H*')[0] | |
| "#{encrypted_hexed}--#{iv_hexed}" | |
| end | |
| private | |
| def self.digest_key(raw_key) | |
| digest = Digest::MD5.new | |
| digest.update(raw_key) | |
| key = digest.digest | |
| key | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment