Created
October 13, 2008 20:46
-
-
Save dramsay/16597 to your computer and use it in GitHub Desktop.
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' | |
module Encryption | |
def encrypt(data, password, salt) | |
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc') | |
cipher.encrypt | |
cipher.pkcs5_keyivgen(password, salt) | |
encrypted_data = cipher.update(data) | |
encrypted_data << cipher.final | |
end | |
def decrypt(encrypted_data, password, salt) | |
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc') | |
cipher.decrypt | |
cipher.pkcs5_keyivgen(password, salt) | |
data = cipher.update(encrypted_data) | |
data << cipher.final | |
end | |
end | |
# Note: you will need to generate the salt and use the same salt and password for both encryption | |
# and decryption | |
# | |
# sample salt generator below | |
# | |
# def generate_salt | |
# [rand(2**64 - 1)].pack("Q") | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment