Created
June 15, 2012 01:54
-
-
Save hugobast/2934195 to your computer and use it in GitHub Desktop.
Keychain
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 'gibberish' | |
class Keychain | |
module Cipher | |
@@secret = "" | |
def self.configuration | |
yield self | |
end | |
def self.secret= secret | |
@@secret = secret | |
end | |
def self.secret | |
@@secret | |
end | |
def self.encrypt information | |
@@cipher ||= Gibberish::AES.new @@secret | |
@@cipher.enc information | |
end | |
def self.decrypt information | |
@@cipher.dec information | |
end | |
end | |
end |
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_relative '../keychain/cipher' | |
describe Keychain::Cipher do | |
it "encrypts a piece of information" do | |
encrypted_text = Keychain::Cipher.encrypt 'some text' | |
encrypted_text.should_not == 'some text' | |
decrypted_text = Keychain::Cipher.decrypt encrypted_text | |
decrypted_text.should == 'some text' | |
end | |
it "secret should be configurable" do | |
Keychain::Cipher.configuration do |config| | |
config.secret = "some other secret" | |
end | |
Keychain::Cipher.secret.should == "some other secret" | |
end | |
end |
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
module Mixin | |
module Key | |
def self.included klass | |
klass.extend ClassMethods | |
end | |
def cipher | |
Keychain::Cipher | |
end | |
def will_lock | |
@@will_unlock | |
end | |
def lock | |
cipher_direction :encrypt | |
end | |
def unlock | |
cipher_direction :decrypt | |
end | |
module ClassMethods | |
def attr_locks *syms | |
syms.each do |sym| | |
attr_accessor sym | |
end | |
Key.class_variable_set :@@will_unlock, syms | |
end | |
end | |
private | |
def cipher_direction direction | |
@@will_unlock.each do |sym| | |
value = instance_variable_get "@#{sym}" | |
processed_value = cipher.method(direction).call value | |
instance_variable_set "@#{sym}", processed_value | |
end | |
end | |
end | |
end |
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_relative '../keychain/key' | |
require_relative '../keychain/cipher' | |
class KeyStore | |
include Mixin::Key | |
attr_locks :login, :password | |
end | |
describe Mixin::Key do | |
let(:key) { KeyStore.new } | |
before do | |
key.login = "john@example.com" | |
key.password = "abc123" | |
end | |
it "has fields to lock" do | |
key.will_lock.should == [:login, :password] | |
end | |
it "unlocks itself" do | |
key.lock | |
key.login.should_not match "john@example.com" | |
key.unlock | |
key.login.should match "john@example.com" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment