Skip to content

Instantly share code, notes, and snippets.

@eterps
Created Apr 22, 2009
Embed
What would you like to do?
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