This repository was archived by the owner on Sep 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy paths2k.rb
More file actions
254 lines (219 loc) · 5.82 KB
/
s2k.rb
File metadata and controls
254 lines (219 loc) · 5.82 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
module OpenPGP
##
# OpenPGP string-to-key (S2K) specifiers.
#
# @see http://tools.ietf.org/html/rfc4880#section-3.7
class S2K
# @return [String]
attr_accessor :passphrase
# @return [Integer]
attr_accessor :algorithm
##
# @param [Buffer] input
# @return [S2K]
def self.parse(input)
case mode = input.read_byte
when 0 then S2K::Simple.parse(input) # Simple S2K
when 1 then S2K::Salted.parse(input) # Salted S2K
when 3 then S2K::Iterated.parse(input) # Iterated and Salted S2K
when 100..110 then S2K.new(:data => input.read) # Private/Experimental S2K
else # TODO
end
end
##
# @return [Integer]
def self.identifier
const_get(:IDENTIFIER)
end
##
# @param [String, #to_s] passphrase
# @param [Hash{Symbol => Object}] options
def initialize(passphrase = nil, options = {}, &block)
@passphrase = passphrase.to_s
options.each { |k, v| instance_variable_set("@#{k}", v) }
block.call(self) if block_given?
end
##
# @param [Buffer] buffer
# @return [void]
def write(buffer)
buffer.write_byte(identifier)
buffer.write_byte(digest.to_i)
end
##
# @return [Integer]
def identifier
@identifier || self.class.identifier
end
##
# @return [Hash]
def to_hash
{:mode => identifier, :algorithm => digest.to_i}
end
##
# @return [String]
def to_s
Buffer.write { |buffer| write(buffer) }
end
##
# @return [Object]
def to_key(key_size = 16)
key = if digest.size >= key_size
digest.digest(digest_input)
else
Buffer.write do |buffer|
(key_size / digest.size.to_f).ceil.times do |i|
buffer << digest.digest(digest_input_with_preload(i))
end
end
end
key[0, key_size]
end
##
# @return [Class]
def digest
@digest ||= case algorithm
when nil then Digest::DEFAULT
when Digest then algorithm
when Symbol then Digest.for(algorithm)
when String then Digest.for(algorithm)
else Digest.for(algorithm.to_i)
end
end
##
# @param [Integer] length
# @return [String]
def digest_input_with_preload(length = 0)
("\0" * length) << digest_input
end
##
# @return [String]
# @raise [NotImplementedError] unless implemented in subclass
# @abstract
def digest_input
raise NotImplementedError
end
##
# @see http://tools.ietf.org/html/rfc4880#section-3.7.1.1
class Simple < S2K
IDENTIFIER = 0x00
##
# @param [Buffer] input
# @return [S2K]
def self.parse(input)
self.new(nil, :algorithm => input.read_byte)
end
##
# @return [String]
def digest_input
passphrase
end
end
##
# @see http://tools.ietf.org/html/rfc4880#section-3.7.1.2
class Salted < S2K
IDENTIFIER = 0x01
##
# @param [Buffer] input
# @return [S2K]
def self.parse(input)
self.new(nil, :algorithm => input.read_byte, :salt => input.read_bytes(8))
end
# @return [String]
attr_accessor :salt
##
# @param [String, #to_s] passphrase
# @param [Hash{Symbol => Object}] options
def initialize(passphrase = nil, options = {}, &block)
super(passphrase, options, &block)
@salt = Random.bytes(8) unless @salt
end
##
# @param [Buffer] buffer
# @return [void]
def write(buffer)
super(buffer)
buffer.write_bytes(salt)
end
##
# @return [Hash]
def to_hash
super.merge({:salt => salt})
end
##
# @return [String]
def digest_input
salt.to_s[0, 8] << passphrase
end
end
##
# @see http://tools.ietf.org/html/rfc4880#section-3.7.1.3
class Iterated < Salted
IDENTIFIER = 0x03
##
# @param [Buffer] input
# @return [S2K]
def self.parse(input)
self.new(nil, :algorithm => input.read_byte, :salt => input.read_bytes(8)) do |s2k|
s2k.count = s2k.decode_count(input.read_byte)
end
end
# @return [Integer]
attr_accessor :count
##
# @param [String, #to_s] passphrase
# @param [Hash{Symbol => Object}] options
def initialize(passphrase = nil, options = {}, &block)
super(passphrase, options, &block)
@count = 65536 unless @count
end
##
# @param [Buffer] buffer
# @return [void]
def write(buffer)
super(buffer)
buffer.write_byte(encode_count(count))
end
##
# @return [Hash]
def to_hash
super.merge(:count => count)
end
##
# @return [String]
def digest_input
buffer = Buffer.write do |buffer|
iterations = count
while iterations > 0
buffer << (digest_input = super())
iterations -= digest_input.size
end
end
end
##
# @param [Integer] count
# @return [Integer]
def decode_count(count)
(16 + (count & 15)) << ((count >> 4) + EXPBIAS)
end
protected
EXPBIAS = 6
##
# @param [Integer] iterations
# @return [Integer]
def encode_count(iterations)
case iterations
when 0..1024 then 0
when 65011712..(1.0/0) then 255
else
count1 = iterations >> 6
count2 = (count2 || 0) + 1 while count1 >= 32 && count1 >>= 1
result = (count2 << 4) | (count1 - 16)
result += 1 if decode_count(result) < iterations
result
end
end
end
DEFAULT = Iterated
end
end