git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Tree: 28e13759ae318586d9c771b02402579b343c97cb

Files: 28e13759ae318586d9c771b02402579b343c97cb / spec / bcrypt / engine_spec.rb

3051 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 class MyInvalidSecret
33 undef to_s
34 end
35
36 before :each do
37 @salt = BCrypt::Engine.generate_salt(4)
38 @password = "woo"
39 end
40
41 specify "should produce a string" do
42 BCrypt::Engine.hash_secret(@password, @salt).should be_an_instance_of(String)
43 end
44
45 specify "should raise an InvalidSalt error if the salt is invalid" do
46 lambda { BCrypt::Engine.hash_secret(@password, 'nino') }.should raise_error(BCrypt::Errors::InvalidSalt)
47 end
48
49 specify "should raise an InvalidSecret error if the secret is invalid" do
50 lambda { BCrypt::Engine.hash_secret(MyInvalidSecret.new, @salt) }.should raise_error(BCrypt::Errors::InvalidSecret)
51 lambda { BCrypt::Engine.hash_secret(nil, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
52 lambda { BCrypt::Engine.hash_secret(false, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
53 end
54
55 specify "should call #to_s on the secret and use the return value as the actual secret data" do
56 BCrypt::Engine.hash_secret(false, @salt).should == BCrypt::Engine.hash_secret("false", @salt)
57 end
58
59 specify "should be interoperable with other implementations" do
60 # test vectors from the OpenWall implementation <http://www.openwall.com/crypt/>
61 test_vectors = [
62 ["U*U", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"],
63 ["U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK"],
64 ["U*U*U", "$2a$05$XXXXXXXXXXXXXXXXXXXXXO", "$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a"],
65 ["", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy"],
66 ["0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "$2a$05$abcdefghijklmnopqrstuu", "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui"]
67 ]
68 for secret, salt, test_vector in test_vectors
69 BCrypt::Engine.hash_secret(secret, salt).should eql(test_vector)
70 end
71 end
72end
73

Built with git-ssb-web