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
class Crypto | |
# encrypts data with the given key. returns a binary data with the | |
# unhashed random iv in the first 16 bytes | |
def self.encrypt(data, key) | |
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") | |
cipher.encrypt | |
cipher.key = key = Digest::SHA256.digest(key) | |
random_iv = cipher.random_iv | |
cipher.iv = Digest::SHA256.digest(random_iv + key)[0..15] | |
encrypted = cipher.update(data) |