Skip to content

Instantly share code, notes, and snippets.

@kajic
Created May 31, 2013 16:11
Show Gist options
  • Save kajic/5686064 to your computer and use it in GitHub Desktop.
Save kajic/5686064 to your computer and use it in GitHub Desktop.
Blowfish encryption and decryption of strings
require 'openssl'
require 'base64'
module Cipher
def self.encrypt(key, data)
data += 'A' # Add 'A' suffix to support empty data
cipher(:encrypt, key, data)
end
def self.decrypt(key, text)
data = cipher(:decrypt, key, text)
data[0...-1] # Remove the 'A' suffix
end
def self.encrypt_base64(key, data)
blowfish_string = self.encrypt(key, data)
Base64.encode64(blowfish_string)
end
def self.decrypt_base64(key, base64_string)
blowfish_string = Base64.decode64(base64_string)
self.decrypt(key, blowfish_string)
end
private
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
end
require 'spec_helper'
describe Cipher do
let(:key) { 'secret key' }
describe 'blowfish' do
it 'encodes and decodes empty strings' do
original = ''
encrypted = Cipher.encrypt(key, original)
decrypted = Cipher.decrypt(key, encrypted)
decrypted.should == original
end
it 'encodes and decodes strings' do
original = 'my string'
encrypted = Cipher.encrypt(key, original)
decrypted = Cipher.decrypt(key, encrypted)
decrypted.should == original
end
end
describe 'base64 and blowfish' do
it 'encodes and decodes empty strings' do
original = ''
encrypted = Cipher.encrypt_base64(key, original)
decrypted = Cipher.decrypt_base64(key, encrypted)
decrypted.should == original
end
it 'encodes and decodes strings' do
original = 'my string'
encrypted = Cipher.encrypt_base64(key, original)
decrypted = Cipher.decrypt_base64(key, encrypted)
decrypted.should == original
end
end
end
@bhaity
Copy link

bhaity commented Mar 11, 2016

updated spec for newer rspec syntax

require 'spec_helper'

describe Cipher do
  let(:key) { 'secret key' }

  describe 'blowfish' do
    it 'encodes and decodes empty strings' do
      original = ''
      encrypted = Cipher.encrypt(key, original)
      decrypted = Cipher.decrypt(key, encrypted)
      expect(decrypted).to eql(original)
    end

    it 'encodes and decodes strings' do
      original = 'my string'
      encrypted = Cipher.encrypt(key, original)
      decrypted = Cipher.decrypt(key, encrypted)
      expect(decrypted).to eql(original)
    end
  end


  describe 'base64 and blowfish' do
    it 'encodes and decodes empty strings' do
      original = ''
      encrypted = Cipher.encrypt_base64(key, original)
      decrypted = Cipher.decrypt_base64(key, encrypted)
      expect(decrypted).to eql(original)
    end

    it 'encodes and decodes strings' do
      original = 'my string'
      encrypted = Cipher.encrypt_base64(key, original)
      decrypted = Cipher.decrypt_base64(key, encrypted)
      expect(decrypted).to eql(original)
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment