Created
May 15, 2009 18:59
-
-
Save benders/112379 to your computer and use it in GitHub Desktop.
Example symmetric encryption (Snipped from phedders / prh-rlib)
This file contains hidden or 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' | |
def simple_decrypt(iv,crypted,password) | |
crypt = OpenSSL::Cipher::Cipher.new("aes-256-cbc") | |
crypt.decrypt | |
crypt.key = Digest::SHA256.digest(password) | |
crypt.iv = iv | |
data = crypt.update(crypted) | |
data << crypt.final rescue nil | |
end | |
def simple_encrypt(iv,data,password) | |
crypted = {} | |
crypt = OpenSSL::Cipher::Cipher.new("aes-256-cbc") | |
crypt.encrypt | |
crypt.key = Digest::SHA256.digest(password) | |
crypt.iv = crypted[:iv] = iv ? iv : crypt.random_iv | |
crypted[:data] = crypt.update(data) | |
crypted[:data] << crypt.final | |
return crypted | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment