forked from defuse/password-hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasswordStorage.rb
142 lines (119 loc) · 3.24 KB
/
PasswordStorage.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Authors: Stephen Chavez (dicesoft.net), Taylor Hornby (defuse.ca).
require 'securerandom'
require 'openssl'
require 'base64'
class InvalidHashError < StandardError
end
class CannotPerformOperationException < StandardError
end
module PasswordStorage
# The following constants can be changed without breaking existing hashes.
PBKDF2_ITERATIONS = 64000
PBKDF2_SALT_BYTES = 24
PBKDF2_OUTPUT_BYTES = 18
# These constants define the encoding and may not be changed.
SECTION_DELIMITER = ':'
HASH_SECTIONS = 5
HASH_ALGORITHM_INDEX = 0
HASH_ITERATIONS_INDEX = 1
HASH_SIZE_INDEX = 2
HASH_SALT_INDEX = 3
HASH_PBKDF2_INDEX = 4
def self.createHash( password )
begin
salt = SecureRandom.random_bytes( PBKDF2_SALT_BYTES )
rescue NotImplementedError
raise CannotPerformOperationException.new(
"Random number generator not available."
)
end
pbkdf2 = pbkdf2_hmac_sha1(
password,
salt,
PBKDF2_ITERATIONS,
PBKDF2_OUTPUT_BYTES
)
parts = [
"sha1",
PBKDF2_ITERATIONS,
pbkdf2.bytesize(),
Base64.strict_encode64( salt ),
Base64.strict_encode64( pbkdf2 )
]
return parts.join( SECTION_DELIMITER )
end
def self.verifyPassword( password, hash )
params = hash.split( SECTION_DELIMITER )
if params.length != HASH_SECTIONS
raise InvalidHashError.new(
"Fields are missing from the password hash."
)
end
if params[HASH_ALGORITHM_INDEX] != "sha1"
raise CannotPerformOperationException.new(
"Unsupported hash type."
)
end
begin
pbkdf2 = Base64.strict_decode64( params[HASH_PBKDF2_INDEX] )
rescue ArgumentError
raise InvalidHashError.new(
"Base64 decoding of pbkdf2 output failed."
)
end
begin
salt = Base64.strict_decode64( params[HASH_SALT_INDEX] )
rescue
raise InvalidHashError.new(
"Base64 decoding of salt failed."
)
end
if pbkdf2.bytesize() != params[HASH_SIZE_INDEX].to_i
raise InvalidHashError.new(
"PBKDF2 output length doesn't match stored output length."
)
end
iterations = params[HASH_ITERATIONS_INDEX].to_i
if iterations < 1
raise InvalidHashError.new(
"Invalid number of iterations. Must be >= 1."
)
end
testOutput = pbkdf2_hmac_sha1(
password,
salt,
iterations,
pbkdf2.bytesize()
)
return slow_equals(pbkdf2, testOutput)
end
def self.pbkdf2_hmac_sha1(password, salt, iterations, byte_length)
begin
return OpenSSL::PKCS5::pbkdf2_hmac_sha1(
password,
salt,
iterations,
byte_length
)
rescue OpenSSL::PKCS5::PKCS5Error
# According to the Ruby source code, if OpenSSL's calculation of PBKDF2
# fails, then it throws "ePKCS5" which I'm *guessing* is this (it's not
# documented explicitly).
raise CannotPerformOperationException.new(
"OpenSSL failed to compute PBKDF2."
)
end
end
def self.slow_equals(a, b)
if a.bytesize() != b.bytesize()
return false
else
result = 0
b_bytes = b.bytes.to_a
a.bytes.each_with_index {|a_byte,i|
result |= a_byte ^ b_bytes[i]
}
return result == 0
end
end
end