Created
April 22, 2009 10:20
-
-
Save eterps/99700 to your computer and use it in GitHub Desktop.
Simple encryption wrapper for Ruby
This file contains 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' | |
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