Files: 8b90c357e99debc6e49cef5bf027c1034d12a097 / spec / bcrypt / engine_spec.rb
2345 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 InvalidSaltError 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 be interoperable with other implementations" do |
46 | # test vectors from the OpenWall implementation <http://www.openwall.com/crypt/> |
47 | test_vectors = [ |
48 | ["U*U", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"], |
49 | ["U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK"], |
50 | ["U*U*U", "$2a$05$XXXXXXXXXXXXXXXXXXXXXO", "$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a"], |
51 | ["", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy"], |
52 | ["0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "$2a$05$abcdefghijklmnopqrstuu", "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui"] |
53 | ] |
54 | for secret, salt, test_vector in test_vectors |
55 | BCrypt::Engine.hash(secret, salt).should eql(test_vector) |
56 | end |
57 | end |
58 | end |
Built with git-ssb-web