Skip to content

Instantly share code, notes, and snippets.

@dux
Created September 27, 2011 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dux/1243964 to your computer and use it in GitHub Desktop.
Save dux/1243964 to your computer and use it in GitHub Desktop.
rails crypt lib - SSL aes 256 cbc
require 'openssl'
require "base64"
class Crypt
def self.sha1(str)
Digest::SHA1.hexdigest str.to_s
end
def self.cipher(mode, data, key='')
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(mode)
cipher.key = Digest::SHA512.digest(ActionController::Base.cookie_verifier_secret+key)
cipher.update(data.to_s) << cipher.final
end
def self.encrypt(data,key='')
Base64.encode64(cipher(:encrypt, data, key)).chomp
end
def self.decrypt(text, key='')
cipher(:decrypt, Base64.decode64(text), key)
end
end
# CloudFlare "aware" IP get
class ActionController::Request
def encrypt(data)
Crypt.encrypt(data, (headers["CF-Connecting-IP"] || remote_ip))
end
def decrypt(text)
Crypt.decrypt(text, (headers["CF-Connecting-IP"] || remote_ip))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment