Created
July 13, 2010 16:47
-
-
Save toland/474152 to your computer and use it in GitHub Desktop.
Simple Ruby module for encrypting text with Blowfish
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' | |
module Blowfish | |
def self.cipher(mode, key, data) | |
cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').send(mode) | |
cipher.key = Digest::SHA256.digest(key) | |
cipher.update(data) << cipher.final | |
end | |
def self.encrypt(key, data) | |
cipher(:encrypt, key, data).unpack('H*').first | |
end | |
def self.decrypt(key, text) | |
cipher(:decrypt, key, [text].pack('H*')) | |
end | |
end | |
if $0 == __FILE__ | |
p "text" == Blowfish.decrypt("key", Blowfish.encrypt("key", "text")) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not making use of the initial vector which is part of CBC. IV is stored outside of the data, in plaintext. For your simple demo, add cipher.iv = 'elsewher' after line 6, or new param iv.