Files: 167b5d49c4c79f96209dd1a236bedf4f04241b76 / spec / bcrypt / engine_spec.rb
2635 bytesRaw
1 | require File.join(File.dirname(__FILE__), "..", "spec_helper") |
2 | |
3 | context "The BCrypt engine" do |
4 | specify "should calculate the optimal cost factor to fit in a specific time" do |
5 | first = BCrypt::Engine.calibrate(100) |
6 | second = BCrypt::Engine.calibrate(300) |
7 | second.should >(first + 1) |
8 | end |
9 | end |
10 | |
11 | context "Generating BCrypt salts" do |
12 | |
13 | specify "should produce strings" do |
14 | BCrypt::Engine.generate_salt.should be_an_instance_of(String) |
15 | end |
16 | |
17 | specify "should produce random data" do |
18 | BCrypt::Engine.generate_salt.should_not equal(BCrypt::Engine.generate_salt) |
19 | end |
20 | |
21 | specify "should raise a InvalidCostError if the cost parameter isn't numeric" do |
22 | lambda { BCrypt::Engine.generate_salt('woo') }.should raise_error(BCrypt::Errors::InvalidCost) |
23 | end |
24 | |
25 | specify "should raise a InvalidCostError if the cost parameter isn't greater than 0" do |
26 | lambda { BCrypt::Engine.generate_salt(-1) }.should raise_error(BCrypt::Errors::InvalidCost) |
27 | end |
28 | end |
29 | |
30 | context "Generating BCrypt hashes" do |
31 | |
32 | setup do |
33 | @salt = BCrypt::Engine.generate_salt(4) |
34 | @password = "woo" |
35 | end |
36 | |
37 | specify "should produce a string" do |
38 | BCrypt::Engine.hash(@password, @salt).should be_an_instance_of(String) |
39 | end |
40 | |
41 | specify "should raise an InvalidSalt error if the salt is invalid" do |
42 | lambda { BCrypt::Engine.hash(@password, 'nino') }.should raise_error(BCrypt::Errors::InvalidSalt) |
43 | end |
44 | |
45 | specify "should raise an InvalidSecret error if the secret is invalid" do |
46 | lambda { BCrypt::Engine.hash(nil, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret) |
47 | lambda { BCrypt::Engine.hash(false, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret) |
48 | end |
49 | |
50 | specify "should be interoperable with other implementations" do |
51 | # test vectors from the OpenWall implementation <http://www.openwall.com/crypt/> |
52 | test_vectors = [ |
53 | ["U*U", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"], |
54 | ["U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK"], |
55 | ["U*U*U", "$2a$05$XXXXXXXXXXXXXXXXXXXXXO", "$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a"], |
56 | ["", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy"], |
57 | ["0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "$2a$05$abcdefghijklmnopqrstuu", "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui"] |
58 | ] |
59 | for secret, salt, test_vector in test_vectors |
60 | BCrypt::Engine.hash(secret, salt).should eql(test_vector) |
61 | end |
62 | end |
63 | end |
Built with git-ssb-web