Skip to content

Instantly share code, notes, and snippets.

@edeustace
Created October 17, 2014 17:32
Show Gist options
  • Select an option

  • Save edeustace/e42db1ee594f0ea44187 to your computer and use it in GitHub Desktop.

Select an option

Save edeustace/e42db1ee594f0ea44187 to your computer and use it in GitHub Desktop.
An example of aes encryption in ruby for CoreSpring
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