git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Tree: 8b90c357e99debc6e49cef5bf027c1034d12a097

Files: 8b90c357e99debc6e49cef5bf027c1034d12a097 / spec / bcrypt / engine_spec.rb

2345 bytesRaw
1require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
3context "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
9end
10
11context "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
28end
29
30context "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
58end

Built with git-ssb-web