git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Commit 32a206ce672a8af55442c22e030e147da01ddde3

Remove trailing whitespace

Aman Gupta committed on 12/20/2010, 6:55:23 PM
Parent: 85cd242425bb89f848cce3fdc24d7f8b1cc6764f

Files changed

lib/bcrypt.rbchanged
spec/bcrypt/engine_spec.rbchanged
spec/bcrypt/password_spec.rbchanged
lib/bcrypt.rbView
@@ -16,34 +16,34 @@
1616 class InvalidHash < StandardError; end # The hash parameter provided to bcrypt() is invalid.
1717 class InvalidCost < StandardError; end # The cost parameter provided to bcrypt() is invalid.
1818 class InvalidSecret < StandardError; end # The secret parameter provided to bcrypt() is invalid.
1919 end
20-
20+
2121 # A Ruby wrapper for the bcrypt() C extension calls and the Java calls.
2222 class Engine
2323 # The default computational expense parameter.
2424 DEFAULT_COST = 10
2525 # The minimum cost supported by the algorithm.
2626 MIN_COST = 4
2727 # Maximum possible size of bcrypt() salts.
2828 MAX_SALT_LENGTH = 16
29-
29+
3030 if RUBY_PLATFORM != "java"
3131 # C-level routines which, if they don't get the right input, will crash the
3232 # hell out of the Ruby process.
3333 private_class_method :__bc_salt
3434 private_class_method :__bc_crypt
3535 end
36-
36+
3737 # Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates
3838 # a bcrypt() password hash.
3939 def self.hash_secret(secret, salt, cost = nil)
4040 if valid_secret?(secret)
4141 if valid_salt?(salt)
4242 if cost.nil?
4343 cost = autodetect_cost(salt)
4444 end
45-
45+
4646 if RUBY_PLATFORM == "java"
4747 Java.bcrypt_jruby.BCrypt.hashpw(secret.to_s, salt.to_s)
4848 else
4949 __bc_crypt(secret.to_s, salt, cost)
@@ -54,9 +54,9 @@
5454 else
5555 raise Errors::InvalidSecret.new("invalid secret")
5656 end
5757 end
58-
58+
5959 # Generates a random salt with a given computational cost.
6060 def self.generate_salt(cost = DEFAULT_COST)
6161 cost = cost.to_i
6262 if cost > 0
@@ -71,29 +71,29 @@
7171 else
7272 raise Errors::InvalidCost.new("cost must be numeric and > 0")
7373 end
7474 end
75-
75+
7676 # Returns true if +salt+ is a valid bcrypt() salt, false if not.
7777 def self.valid_salt?(salt)
7878 salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/
7979 end
80-
80+
8181 # Returns true if +secret+ is a valid bcrypt() secret, false if not.
8282 def self.valid_secret?(secret)
8383 secret.respond_to?(:to_s)
8484 end
85-
85+
8686 # Returns the cost factor which will result in computation times less than +upper_time_limit_in_ms+.
87- #
87+ #
8888 # Example:
89- #
89+ #
9090 # BCrypt.calibrate(200) #=> 10
9191 # BCrypt.calibrate(1000) #=> 12
92- #
92+ #
9393 # # should take less than 200ms
9494 # BCrypt::Password.create("woo", :cost => 10)
95- #
95+ #
9696 # # should take less than 1000ms
9797 # BCrypt::Password.create("woo", :cost => 12)
9898 def self.calibrate(upper_time_limit_in_ms)
9999 40.times do |i|
@@ -102,36 +102,36 @@
102102 end_time = Time.now - start_time
103103 return i if end_time * 1_000 > upper_time_limit_in_ms
104104 end
105105 end
106-
106+
107107 # Autodetects the cost from the salt string.
108108 def self.autodetect_cost(salt)
109109 salt[4..5].to_i
110110 end
111111 end
112-
112+
113113 # A password management class which allows you to safely store users' passwords and compare them.
114- #
114+ #
115115 # Example usage:
116- #
116+ #
117117 # include BCrypt
118- #
118+ #
119119 # # hash a user's password
120120 # @password = Password.create("my grand secret")
121121 # @password #=> "$2a$10$GtKs1Kbsig8ULHZzO1h2TetZfhO4Fmlxphp8bVKnUlZCBYYClPohG"
122- #
122+ #
123123 # # store it safely
124124 # @user.update_attribute(:password, @password)
125- #
125+ #
126126 # # read it back
127127 # @user.reload!
128128 # @db_password = Password.new(@user.password)
129- #
129+ #
130130 # # compare it after retrieval
131131 # @db_password == "my grand secret" #=> true
132132 # @db_password == "a paltry guess" #=> false
133- #
133+ #
134134 class Password < String
135135 # The hash portion of the stored password hash.
136136 attr_reader :hash
137137 # The salt of the store password hash (including version and cost).
@@ -139,24 +139,24 @@
139139 # The version of the bcrypt() algorithm used to create the hash.
140140 attr_reader :version
141141 # The cost factor used to create the hash.
142142 attr_reader :cost
143-
143+
144144 class << self
145145 # Hashes a secret, returning a BCrypt::Password instance. Takes an optional <tt>:cost</tt> option, which is a
146146 # logarithmic variable which determines how computational expensive the hash is to calculate (a <tt>:cost</tt> of
147147 # 4 is twice as much work as a <tt>:cost</tt> of 3). The higher the <tt>:cost</tt> the harder it becomes for
148148 # attackers to try to guess passwords (even if a copy of your database is stolen), but the slower it is to check
149149 # users' passwords.
150- #
150+ #
151151 # Example:
152- #
152+ #
153153 # @password = BCrypt::Password.create("my secret", :cost => 13)
154154 def create(secret, options = { :cost => BCrypt::Engine::DEFAULT_COST })
155155 Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(options[:cost]), options[:cost]))
156156 end
157157 end
158-
158+
159159 # Initializes a BCrypt::Password instance with the data from a stored hash.
160160 def initialize(raw_hash)
161161 if valid_hash?(raw_hash)
162162 self.replace(raw_hash)
@@ -164,24 +164,24 @@
164164 else
165165 raise Errors::InvalidHash.new("invalid hash")
166166 end
167167 end
168-
168+
169169 # Compares a potential secret against the hash. Returns true if the secret is the original secret, false otherwise.
170170 def ==(secret)
171171 super(BCrypt::Engine.hash_secret(secret, @salt))
172172 end
173173 alias_method :is_password?, :==
174-
174+
175175 private
176176 # Returns true if +h+ is a valid hash.
177177 def valid_hash?(h)
178178 h =~ /^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/
179179 end
180-
180+
181181 # call-seq:
182182 # split_hash(raw_hash) -> version, cost, salt, hash
183- #
183+ #
184184 # Splits +h+ into version, cost, salt, and hash and returns them in that order.
185185 def split_hash(h)
186186 b, v, c, mash = h.split('$')
187187 return v, c.to_i, h[0, 29], mash[-31, 31]
spec/bcrypt/engine_spec.rbView
@@ -8,65 +8,65 @@
88 end
99 end
1010
1111 describe "Generating BCrypt salts" do
12-
12+
1313 specify "should produce strings" do
1414 BCrypt::Engine.generate_salt.should be_an_instance_of(String)
1515 end
16-
16+
1717 specify "should produce random data" do
1818 BCrypt::Engine.generate_salt.should_not equal(BCrypt::Engine.generate_salt)
1919 end
20-
20+
2121 specify "should raise a InvalidCostError if the cost parameter isn't numeric" do
2222 lambda { BCrypt::Engine.generate_salt('woo') }.should raise_error(BCrypt::Errors::InvalidCost)
2323 end
24-
24+
2525 specify "should raise a InvalidCostError if the cost parameter isn't greater than 0" do
2626 lambda { BCrypt::Engine.generate_salt(-1) }.should raise_error(BCrypt::Errors::InvalidCost)
2727 end
2828 end
2929
3030 describe "Autodetecting of salt cost" do
31-
31+
3232 specify "should work" do
3333 BCrypt::Engine.autodetect_cost("$2a$08$hRx2IVeHNsTSYYtUWn61Ou").should == 8
3434 BCrypt::Engine.autodetect_cost("$2a$05$XKd1bMnLgUnc87qvbAaCUu").should == 5
3535 BCrypt::Engine.autodetect_cost("$2a$13$Lni.CZ6z5A7344POTFBBV.").should == 13
3636 end
37-
37+
3838 end
3939
4040 describe "Generating BCrypt hashes" do
41-
41+
4242 class MyInvalidSecret
4343 undef to_s
4444 end
45-
45+
4646 before :each do
4747 @salt = BCrypt::Engine.generate_salt(4)
4848 @password = "woo"
4949 end
50-
50+
5151 specify "should produce a string" do
5252 BCrypt::Engine.hash_secret(@password, @salt).should be_an_instance_of(String)
5353 end
54-
54+
5555 specify "should raise an InvalidSalt error if the salt is invalid" do
5656 lambda { BCrypt::Engine.hash_secret(@password, 'nino') }.should raise_error(BCrypt::Errors::InvalidSalt)
5757 end
58-
58+
5959 specify "should raise an InvalidSecret error if the secret is invalid" do
6060 lambda { BCrypt::Engine.hash_secret(MyInvalidSecret.new, @salt) }.should raise_error(BCrypt::Errors::InvalidSecret)
6161 lambda { BCrypt::Engine.hash_secret(nil, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
6262 lambda { BCrypt::Engine.hash_secret(false, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
6363 end
64-
64+
6565 specify "should call #to_s on the secret and use the return value as the actual secret data" do
6666 BCrypt::Engine.hash_secret(false, @salt).should == BCrypt::Engine.hash_secret("false", @salt)
6767 end
68-
68+
6969 specify "should be interoperable with other implementations" do
7070 # test vectors from the OpenWall implementation <http://www.openwall.com/crypt/>
7171 test_vectors = [
7272 ["U*U", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"],
spec/bcrypt/password_spec.rbView
@@ -1,27 +1,27 @@
11 require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
22
33 describe "Creating a hashed password" do
4-
4+
55 before :each do
66 @secret = "wheedle"
77 @password = BCrypt::Password.create(@secret, :cost => 4)
88 end
9-
9+
1010 specify "should return a BCrypt::Password" do
1111 @password.should be_an_instance_of(BCrypt::Password)
1212 end
13-
13+
1414 specify "should return a valid bcrypt password" do
1515 lambda { BCrypt::Password.new(@password) }.should_not raise_error
1616 end
17-
17+
1818 specify "should behave normally if the secret is not a string" do
1919 lambda { BCrypt::Password.create(nil) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
2020 lambda { BCrypt::Password.create({:woo => "yeah"}) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
2121 lambda { BCrypt::Password.create(false) }.should_not raise_error(BCrypt::Errors::InvalidSecret)
2222 end
23-
23+
2424 specify "should tolerate empty string secrets" do
2525 lambda { BCrypt::Password.create( "\n".chop ) }.should_not raise_error
2626 lambda { BCrypt::Password.create( "" ) }.should_not raise_error
2727 lambda { BCrypt::Password.create( String.new ) }.should_not raise_error
@@ -32,17 +32,17 @@
3232 before :each do
3333 @secret = "U*U"
3434 @hash = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"
3535 end
36-
36+
3737 specify "should read the version, cost, salt, and hash" do
3838 password = BCrypt::Password.new(@hash)
3939 password.version.should eql("2a")
4040 password.cost.should equal(5)
4141 password.salt.should eql("$2a$05$CCCCCCCCCCCCCCCCCCCCC.")
4242 password.to_s.should eql(@hash)
4343 end
44-
44+
4545 specify "should raise an InvalidHashError when given an invalid hash" do
4646 lambda { BCrypt::Password.new('weedle') }.should raise_error(BCrypt::Errors::InvalidHash)
4747 end
4848 end
@@ -52,13 +52,13 @@
5252 @secret = "U*U"
5353 @hash = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"
5454 @password = BCrypt::Password.create(@secret)
5555 end
56-
56+
5757 specify "should compare successfully to the original secret" do
5858 (@password == @secret).should be(true)
5959 end
60-
60+
6161 specify "should compare unsuccessfully to anything besides original secret" do
6262 (@password == "@secret").should be(false)
6363 end
6464 end

Built with git-ssb-web