Skip to content

Instantly share code, notes, and snippets.

@eterps
Created April 22, 2009 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eterps/99700 to your computer and use it in GitHub Desktop.
Save eterps/99700 to your computer and use it in GitHub Desktop.
Simple encryption wrapper for Ruby
require 'openssl'
require 'digest/sha1'
require 'base64'
class Encryption
def self.encrypt(src, key, cipher = 'aes-256-cbc')
c = OpenSSL::Cipher::Cipher.new(cipher)
c.encrypt
c.key = Digest::SHA1.hexdigest(key)
e = c.update(src)
e << c.final
Base64.encode64(e)
end
def self.decrypt(src, key, cipher = 'aes-256-cbc')
c = OpenSSL::Cipher::Cipher.new(cipher)
c.decrypt
c.key = Digest::SHA1.hexdigest(key)
e = Base64.decode64(src)
d = c.update(e)
d << c.final
return d
end
end
src = 'Hello world!'
key = 'yourpass'
tmp = Encryption.encrypt(src, key)
puts tmp
dst = Encryption.decrypt(tmp, key)
puts dst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment