-
Notifications
You must be signed in to change notification settings - Fork 200
/
pbkdf2.rb
167 lines (146 loc) · 4.53 KB
/
pbkdf2.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Modified by Salvatore Sanfilippo to only support HMAC-SHA1 from
# ruby-hmac gem in order to drop the openssl dependency.
#
# Copyright (c) 2008 Sam Quigley <quigley@emerose.com>
# Copyright (c) 2011 Salvatore Sanfilippo <antirez@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# This license is sometimes referred to as "The MIT License"
require 'rubygems'
require 'hmac-sha1'
class PBKDF2
def initialize(opts={})
# override with options
opts.each_key do |k|
if self.respond_to?("#{k}=")
self.send("#{k}=", opts[k])
else
raise ArgumentError, "Argument '#{k}' is not allowed"
end
end
yield self if block_given?
# set this to the default if nothing was given
@key_length ||= 20
# make sure the relevant things got set
raise ArgumentError, "password not set" if @password.nil?
raise ArgumentError, "salt not set" if @salt.nil?
raise ArgumentError, "iterations not set" if @iterations.nil?
end
attr_reader :key_length, :iterations, :salt, :password
def key_length=(l)
raise ArgumentError, "key too short" if l < 1
raise ArgumentError, "key too long" if l > ((2**32 - 1) * 20)
@value = nil
@key_length = l
end
def iterations=(i)
raise ArgumentError, "iterations can't be less than 1" if i < 1
@value = nil
@iterations = i
end
def salt=(s)
@value = nil
@salt = s
end
def password=(p)
@value = nil
@password = p
end
def value
calculate! if @value.nil?
@value
end
alias bin_string value
def hex_string
bin_string.unpack("H*").first
end
# return number of milliseconds it takes to complete one iteration
def benchmark(iters = 400000)
iter_orig = @iterations
@iterations=iters
start = Time.now
calculate!
time = Time.now - start
@iterations = iter_orig
return (time/iters)
end
protected
# the pseudo-random function defined in the spec
def prf(data)
if defined?(OpenSSL)
OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha1"),@password,data)
else
HMAC::SHA1.digest(@password,data)
end
end
# this is a translation of the helper function "F" defined in the spec
def calculate_block(block_num)
# u_1:
u = prf(salt+[block_num].pack("N"))
ret = u
# u_2 through u_c:
2.upto(@iterations) do
# calculate u_n
u = prf(u)
# xor it with the previous results
ret = ret^u
end
ret
end
# the bit that actually does the calculating
def calculate!
# how many blocks we'll need to calculate (the last may be truncated)
blocks_needed = (@key_length.to_f / 20).ceil
# reset
@value = ""
# main block-calculating loop:
1.upto(blocks_needed) do |block_num|
@value << calculate_block(block_num)
end
# truncate to desired length:
@value = @value.slice(0,@key_length)
@value
end
end
class String
if RUBY_VERSION >= "1.9"
def xor_impl(other)
result = "".encode("ASCII-8BIT")
o_bytes = other.bytes.to_a
bytes.each_with_index do |c, i|
result << (c ^ o_bytes[i])
end
result
end
else
def xor_impl(other)
result = (0..self.length-1).collect { |i| self[i] ^ other[i] }
result.pack("C*")
end
end
private :xor_impl
def ^(other)
raise ArgumentError, "Can't bitwise-XOR a String with a non-String" \
unless other.kind_of? String
raise ArgumentError, "Can't bitwise-XOR strings of different length" \
unless self.length == other.length
xor_impl(other)
end
end