Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added in the Cryptify module for password encryption with specs. Firs…
…t step towards HTTP Basic.
- Loading branch information
Michael Bleigh
committed
Mar 19, 2009
1 parent
208c0ef
commit 4088ed0
Showing
11 changed files
with
134 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module TwitterAuth | ||
module Cryptify | ||
class Error < StandardError; end | ||
|
||
def self.encrypt(data) | ||
salt = generate_salt | ||
{:encrypted_data => EzCrypto::Key.encrypt_with_password(TwitterAuth.encryption_key, salt, data), :salt => salt} | ||
end | ||
|
||
def self.decrypt(encrypted_data_or_hash, salt=nil) | ||
case encrypted_data_or_hash | ||
when String | ||
encrypted_data = encrypted_data_or_hash | ||
raise ArgumentError, 'Must provide a salt to decrypt.' unless salt | ||
when Hash | ||
encrypted_data = encrypted_data_or_hash[:encrypted_data] | ||
salt = encrypted_data_or_hash[:salt] | ||
else | ||
raise ArgumentError, 'Must provide either an encrypted hash result or encrypted string and salt.' | ||
end | ||
|
||
EzCrypto::Key.decrypt_with_password(TwitterAuth.encryption_key, salt, encrypted_data) | ||
end | ||
|
||
def self.generate_salt | ||
ActiveSupport::SecureRandom.hex(4) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Gem Dependencies | ||
config.gem 'oauth' | ||
config.gem 'ezcrypto' | ||
|
||
require 'json' | ||
require 'twitter_auth' | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
describe TwitterAuth::Cryptify do | ||
before do | ||
stub_basic! | ||
end | ||
|
||
it 'should have encrypt and decrypt methods' do | ||
TwitterAuth::Cryptify.should respond_to(:encrypt) | ||
TwitterAuth::Cryptify.should respond_to(:decrypt) | ||
end | ||
|
||
describe '.encrypt' do | ||
it 'should return a hash with :encrypted_data and :salt keys' do | ||
result = TwitterAuth::Cryptify.encrypt('some string') | ||
result.should be_a(Hash) | ||
result.key?(:encrypted_data).should be_true | ||
result.key?(:salt).should be_true | ||
end | ||
|
||
it 'should make a call to EzCrypto::Key.encrypt_with_password' do | ||
EzCrypto::Key.should_receive(:encrypt_with_password).once.and_return('gobbledygook') | ||
TwitterAuth::Cryptify.encrypt('some string') | ||
end | ||
|
||
it 'should not have the same encrypted as plaintext data' do | ||
TwitterAuth::Cryptify.encrypt('some string')[:encrypted_data].should_not == 'some string' | ||
end | ||
end | ||
|
||
describe '.decrypt' do | ||
before do | ||
@salt = TwitterAuth::Cryptify.generate_salt | ||
TwitterAuth::Cryptify.stub!(:generate_salt).and_return(@salt) | ||
@string = 'decrypted string' | ||
@encrypted = TwitterAuth::Cryptify.encrypt(@string) | ||
end | ||
|
||
it 'should return the original string' do | ||
TwitterAuth::Cryptify.decrypt(@encrypted).should == @string | ||
end | ||
|
||
it 'should raise an argument error if encrypted data is provided without a salt' do | ||
lambda{TwitterAuth::Cryptify.decrypt('asodiaoie2')}.should raise_error(ArgumentError) | ||
end | ||
|
||
it 'should raise an argument error if a string or hash are not provided' do | ||
lambda{TwitterAuth::Cryptify.decrypt(23)}.should raise_error(ArgumentError) | ||
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