krypt 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +82 -0
- data/lib/krypt.rb +49 -0
- data/lib/krypt/asn1.rb +3 -0
- data/lib/krypt/asn1/common.rb +96 -0
- data/lib/krypt/asn1/template.rb +257 -0
- data/lib/krypt/codec.rb +57 -0
- data/lib/krypt/codec/base64.rb +140 -0
- data/lib/krypt/codec/base_codec.rb +36 -0
- data/lib/krypt/codec/hex.rb +122 -0
- data/lib/krypt/digest.rb +112 -0
- data/lib/krypt/hmac.rb +69 -0
- data/lib/krypt/pkcs5.rb +1 -0
- data/lib/krypt/pkcs5/pbkdf2.rb +41 -0
- data/lib/krypt/provider.rb +35 -0
- data/lib/krypt/x509.rb +3 -0
- data/lib/krypt/x509/certificate.rb +36 -0
- data/lib/krypt/x509/common.rb +41 -0
- data/lib/krypt/x509/crl.rb +33 -0
- data/lib/krypt_missing.rb +32 -0
- data/spec/krypt-core/MEMO.txt +85 -0
- data/spec/krypt-core/asn1/asn1_bit_string_spec.rb +475 -0
- data/spec/krypt-core/asn1/asn1_boolean_spec.rb +392 -0
- data/spec/krypt-core/asn1/asn1_constants_spec.rb +71 -0
- data/spec/krypt-core/asn1/asn1_data_spec.rb +1153 -0
- data/spec/krypt-core/asn1/asn1_end_of_contents_spec.rb +133 -0
- data/spec/krypt-core/asn1/asn1_enumerated_spec.rb +458 -0
- data/spec/krypt-core/asn1/asn1_generalized_time_spec.rb +492 -0
- data/spec/krypt-core/asn1/asn1_integer_spec.rb +557 -0
- data/spec/krypt-core/asn1/asn1_null_spec.rb +360 -0
- data/spec/krypt-core/asn1/asn1_object_id_spec.rb +495 -0
- data/spec/krypt-core/asn1/asn1_octet_string_spec.rb +456 -0
- data/spec/krypt-core/asn1/asn1_parser_spec.rb +503 -0
- data/spec/krypt-core/asn1/asn1_pem_spec.rb +282 -0
- data/spec/krypt-core/asn1/asn1_sequence_spec.rb +637 -0
- data/spec/krypt-core/asn1/asn1_set_spec.rb +795 -0
- data/spec/krypt-core/asn1/asn1_utc_time_spec.rb +495 -0
- data/spec/krypt-core/asn1/asn1_utf8_string_spec.rb +404 -0
- data/spec/krypt-core/asn1/resources.rb +53 -0
- data/spec/krypt-core/base64/base64_spec.rb +97 -0
- data/spec/krypt-core/digest/digest_spec.rb +707 -0
- data/spec/krypt-core/hex/hex_spec.rb +102 -0
- data/spec/krypt-core/pem/pem_decode_spec.rb +235 -0
- data/spec/krypt-core/resources.rb +1 -0
- data/spec/krypt-core/template/template_choice_parse_spec.rb +289 -0
- data/spec/krypt-core/template/template_dsl_spec.rb +351 -0
- data/spec/krypt-core/template/template_seq_of_parse_spec.rb +64 -0
- data/spec/krypt-core/template/template_seq_parse_spec.rb +1241 -0
- data/spec/krypt/codec/base64_decoder_spec.rb +94 -0
- data/spec/krypt/codec/base64_encoder_spec.rb +94 -0
- data/spec/krypt/codec/base64_mixed_spec.rb +16 -0
- data/spec/krypt/codec/hex_decoder_spec.rb +94 -0
- data/spec/krypt/codec/hex_encoder_spec.rb +94 -0
- data/spec/krypt/codec/hex_mixed_spec.rb +17 -0
- data/spec/krypt/codec/identity_shared.rb +119 -0
- data/spec/krypt/hmac/hmac_spec.rb +311 -0
- data/spec/krypt/pkcs5/pbkdf2_spec.rb +79 -0
- data/spec/krypt/provider/provider_spec.rb +83 -0
- data/spec/res/ca-bundle.crt +11758 -0
- data/spec/res/certificate.cer +0 -0
- data/spec/res/certificate.pem +20 -0
- data/spec/res/multiple_certs.pem +60 -0
- data/spec/resources.rb +66 -0
- data/test/helper.rb +8 -0
- data/test/res/certificate.cer +0 -0
- data/test/resources.rb +48 -0
- data/test/scratch.rb +28 -0
- data/test/test_krypt_asn1.rb +119 -0
- data/test/test_krypt_parser.rb +331 -0
- metadata +134 -0
@@ -0,0 +1,311 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'krypt'
|
3
|
+
|
4
|
+
describe "Krypt::HMAC" do
|
5
|
+
|
6
|
+
let(:hmac) { Krypt::HMAC }
|
7
|
+
|
8
|
+
describe "#digest" do
|
9
|
+
it "returns a String with binary encoding" do
|
10
|
+
mac = hmac.new(Krypt::Digest::SHA1.new, "key")
|
11
|
+
mac << "test"
|
12
|
+
mac.digest.encoding.should == Encoding::BINARY
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#hex_digest" do
|
17
|
+
it "returns a String with US-ASCII encoding" do
|
18
|
+
mac = hmac.new(Krypt::Digest::SHA1.new, "key")
|
19
|
+
mac << "test"
|
20
|
+
mac.hexdigest.encoding.should == Encoding::US_ASCII
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "conforms to RFC2202 test vectors" do
|
25
|
+
context "MD5" do
|
26
|
+
let(:binary) { hmac.digest(Krypt::Digest::MD5.new, key, data) }
|
27
|
+
|
28
|
+
context "test_case 1" do
|
29
|
+
let(:key) { Krypt::Hex.decode "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b" }
|
30
|
+
let(:data) { "Hi There" }
|
31
|
+
let(:expected) { "9294727a3638bb1c13f48ef8158bfc9d" }
|
32
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
33
|
+
end
|
34
|
+
|
35
|
+
context "test_case 2" do
|
36
|
+
let(:key) { "Jefe" }
|
37
|
+
let(:data) { "what do ya want for nothing?" }
|
38
|
+
let(:expected) { "750c783e6ab0b503eaa86e310a5db738" }
|
39
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
40
|
+
end
|
41
|
+
|
42
|
+
context "test_case 3" do
|
43
|
+
let(:key) { Krypt::Hex.decode "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }
|
44
|
+
let(:data) { "\xdd" * 50 }
|
45
|
+
let(:expected) { "56be34521d144c88dbb8c733f0e8b3f6" }
|
46
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
47
|
+
end
|
48
|
+
|
49
|
+
context "test_case 4" do
|
50
|
+
let(:key) { Krypt::Hex.decode "0102030405060708090a0b0c0d0e0f10111213141516171819" }
|
51
|
+
let(:data) { "\xcd" * 50 }
|
52
|
+
let(:expected) { "697eaf0aca3a3aea3a75164746ffaa79" }
|
53
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
54
|
+
end
|
55
|
+
|
56
|
+
context "test_case 5" do
|
57
|
+
let(:key) { Krypt::Hex.decode "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c" }
|
58
|
+
let(:data) { "Test With Truncation" }
|
59
|
+
let(:expected) { "56461ef2342edc00f9bab995690efd4c" }
|
60
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
61
|
+
end
|
62
|
+
|
63
|
+
context "test_case 6" do
|
64
|
+
let(:key) { "\xaa" * 80 }
|
65
|
+
let(:data) { "Test Using Larger Than Block-Size Key - Hash Key First" }
|
66
|
+
let(:expected) { "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd" }
|
67
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
68
|
+
end
|
69
|
+
|
70
|
+
context "test_case 7" do
|
71
|
+
let(:key) { "\xaa" * 80 }
|
72
|
+
let(:data) { "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" }
|
73
|
+
let(:expected) { "6f630fad67cda0ee1fb1f562db3aa53e" }
|
74
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context "SHA1" do
|
79
|
+
let(:binary) { hmac.digest(Krypt::Digest::SHA1.new, key, data) }
|
80
|
+
|
81
|
+
context "test_case 1" do
|
82
|
+
let(:key) { Krypt::Hex.decode "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b" }
|
83
|
+
let(:data) { "Hi There" }
|
84
|
+
let(:expected) { "b617318655057264e28bc0b6fb378c8ef146be00" }
|
85
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
86
|
+
end
|
87
|
+
|
88
|
+
context "test_case 2" do
|
89
|
+
let(:key) { "Jefe" }
|
90
|
+
let(:data) { "what do ya want for nothing?" }
|
91
|
+
let(:expected) { "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79" }
|
92
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
93
|
+
end
|
94
|
+
|
95
|
+
context "test_case 3" do
|
96
|
+
let(:key) { Krypt::Hex.decode "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }
|
97
|
+
let(:data) { "\xdd" * 50 }
|
98
|
+
let(:expected) { "125d7342b9ac11cd91a39af48aa17b4f63f175d3" }
|
99
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
100
|
+
end
|
101
|
+
|
102
|
+
context "test_case 4" do
|
103
|
+
let(:key) { Krypt::Hex.decode "0102030405060708090a0b0c0d0e0f10111213141516171819" }
|
104
|
+
let(:data) { "\xcd" * 50 }
|
105
|
+
let(:expected) { "4c9007f4026250c6bc8414f9bf50c86c2d7235da" }
|
106
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
107
|
+
end
|
108
|
+
|
109
|
+
context "test_case 5" do
|
110
|
+
let(:key) { Krypt::Hex.decode "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c" }
|
111
|
+
let(:data) { "Test With Truncation" }
|
112
|
+
let(:expected) { "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04" }
|
113
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
114
|
+
end
|
115
|
+
|
116
|
+
context "test_case 6" do
|
117
|
+
let(:key) { "\xaa" * 80 }
|
118
|
+
let(:data) { "Test Using Larger Than Block-Size Key - Hash Key First" }
|
119
|
+
let(:expected) { "aa4ae5e15272d00e95705637ce8a3b55ed402112" }
|
120
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
121
|
+
end
|
122
|
+
|
123
|
+
context "test_case 7" do
|
124
|
+
let(:key) { "\xaa" * 80 }
|
125
|
+
let(:data) { "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" }
|
126
|
+
let(:expected) { "e8e99d0f45237d786d6bbaa7965c7808bbff1a91" }
|
127
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context "conforms to RFC4231 test vectors" do
|
133
|
+
subject { hmac.hexdigest(digest, key, data) }
|
134
|
+
|
135
|
+
context "Test case 1" do
|
136
|
+
let(:key) { Krypt::Hex.decode "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b" }
|
137
|
+
let(:data) { "Hi There" }
|
138
|
+
|
139
|
+
context "SHA-224" do
|
140
|
+
let(:digest) { Krypt::Digest::SHA224.new }
|
141
|
+
it { subject.should == "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22" }
|
142
|
+
end
|
143
|
+
|
144
|
+
context "SHA-256" do
|
145
|
+
let(:digest) { Krypt::Digest::SHA256.new }
|
146
|
+
it { subject.should == "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" }
|
147
|
+
end
|
148
|
+
|
149
|
+
context "SHA-384" do
|
150
|
+
let(:digest) { Krypt::Digest::SHA384.new }
|
151
|
+
it { subject.should == "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6" }
|
152
|
+
end
|
153
|
+
|
154
|
+
context "SHA-512" do
|
155
|
+
let(:digest) { Krypt::Digest::SHA512.new }
|
156
|
+
it { subject.should == "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854" }
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
context "Test case 2" do
|
161
|
+
let(:key) { "Jefe" }
|
162
|
+
let(:data) { "what do ya want for nothing?" }
|
163
|
+
|
164
|
+
context "SHA-224" do
|
165
|
+
let(:digest) { Krypt::Digest::SHA224.new }
|
166
|
+
it { subject.should == "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44" }
|
167
|
+
end
|
168
|
+
|
169
|
+
context "SHA-256" do
|
170
|
+
let(:digest) { Krypt::Digest::SHA256.new }
|
171
|
+
it { subject.should == "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" }
|
172
|
+
end
|
173
|
+
|
174
|
+
context "SHA-384" do
|
175
|
+
let(:digest) { Krypt::Digest::SHA384.new }
|
176
|
+
it { subject.should == "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649" }
|
177
|
+
end
|
178
|
+
|
179
|
+
context "SHA-512" do
|
180
|
+
let(:digest) { Krypt::Digest::SHA512.new }
|
181
|
+
it { subject.should == "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737" }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context "Test case 3" do
|
186
|
+
let(:key) { Krypt::Hex.decode "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }
|
187
|
+
let(:data) { "\xdd" * 50 }
|
188
|
+
|
189
|
+
context "SHA-224" do
|
190
|
+
let(:digest) { Krypt::Digest::SHA224.new }
|
191
|
+
it { subject.should == "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea" }
|
192
|
+
end
|
193
|
+
|
194
|
+
context "SHA-256" do
|
195
|
+
let(:digest) { Krypt::Digest::SHA256.new }
|
196
|
+
it { subject.should == "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe" }
|
197
|
+
end
|
198
|
+
|
199
|
+
context "SHA-384" do
|
200
|
+
let(:digest) { Krypt::Digest::SHA384.new }
|
201
|
+
it { subject.should == "88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e3ab6e101a34f27" }
|
202
|
+
end
|
203
|
+
|
204
|
+
context "SHA-512" do
|
205
|
+
let(:digest) { Krypt::Digest::SHA512.new }
|
206
|
+
it { subject.should == "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb" }
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context "Test case 4" do
|
211
|
+
let(:key) { Krypt::Hex.decode "0102030405060708090a0b0c0d0e0f10111213141516171819" }
|
212
|
+
let(:data) { "\xcd" * 50 }
|
213
|
+
|
214
|
+
context "SHA-224" do
|
215
|
+
let(:digest) { Krypt::Digest::SHA224.new }
|
216
|
+
it { subject.should == "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a" }
|
217
|
+
end
|
218
|
+
|
219
|
+
context "SHA-256" do
|
220
|
+
let(:digest) { Krypt::Digest::SHA256.new }
|
221
|
+
it { subject.should == "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b" }
|
222
|
+
end
|
223
|
+
|
224
|
+
context "SHA-384" do
|
225
|
+
let(:digest) { Krypt::Digest::SHA384.new }
|
226
|
+
it { subject.should == "3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679ccf8a386c674cffb" }
|
227
|
+
end
|
228
|
+
|
229
|
+
context "SHA-512" do
|
230
|
+
let(:digest) { Krypt::Digest::SHA512.new }
|
231
|
+
it { subject.should == "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd" }
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
context "Test case 5" do
|
236
|
+
let(:key) { Krypt::Hex.decode "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c" }
|
237
|
+
let(:data) { "Test With Truncation" }
|
238
|
+
|
239
|
+
context "SHA-224" do
|
240
|
+
let(:digest) { Krypt::Digest::SHA224.new }
|
241
|
+
it { subject.slice(0, 32).should == "0e2aea68a90c8d37c988bcdb9fca6fa8" }
|
242
|
+
end
|
243
|
+
|
244
|
+
context "SHA-256" do
|
245
|
+
let(:digest) { Krypt::Digest::SHA256.new }
|
246
|
+
it { subject.slice(0, 32).should == "a3b6167473100ee06e0c796c2955552b" }
|
247
|
+
end
|
248
|
+
|
249
|
+
context "SHA-384" do
|
250
|
+
let(:digest) { Krypt::Digest::SHA384.new }
|
251
|
+
it { subject.slice(0, 32).should == "3abf34c3503b2a23a46efc619baef897" }
|
252
|
+
end
|
253
|
+
|
254
|
+
context "SHA-512" do
|
255
|
+
let(:digest) { Krypt::Digest::SHA512.new }
|
256
|
+
it { subject.slice(0, 32).should == "415fad6271580a531d4179bc891d87a6" }
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
context "Test case 6" do
|
261
|
+
let(:key) { "\xaa" * 131 }
|
262
|
+
let(:data) { "Test Using Larger Than Block-Size Key - Hash Key First" }
|
263
|
+
|
264
|
+
context "SHA-224" do
|
265
|
+
let(:digest) { Krypt::Digest::SHA224.new }
|
266
|
+
it { subject.should == "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e" }
|
267
|
+
end
|
268
|
+
|
269
|
+
context "SHA-256" do
|
270
|
+
let(:digest) { Krypt::Digest::SHA256.new }
|
271
|
+
it { subject.should == "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54" }
|
272
|
+
end
|
273
|
+
|
274
|
+
context "SHA-384" do
|
275
|
+
let(:digest) { Krypt::Digest::SHA384.new }
|
276
|
+
it { subject.should == "4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296248df163f44952" }
|
277
|
+
end
|
278
|
+
|
279
|
+
context "SHA-512" do
|
280
|
+
let(:digest) { Krypt::Digest::SHA512.new }
|
281
|
+
it { subject.should == "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598" }
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
context "Test case 7" do
|
286
|
+
let(:key) { "\xaa" * 131 }
|
287
|
+
let(:data) { "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." }
|
288
|
+
|
289
|
+
context "SHA-224" do
|
290
|
+
let(:digest) { Krypt::Digest::SHA224.new }
|
291
|
+
it { subject.should == "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1" }
|
292
|
+
end
|
293
|
+
|
294
|
+
context "SHA-256" do
|
295
|
+
let(:digest) { Krypt::Digest::SHA256.new }
|
296
|
+
it { subject.should == "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2" }
|
297
|
+
end
|
298
|
+
|
299
|
+
context "SHA-384" do
|
300
|
+
let(:digest) { Krypt::Digest::SHA384.new }
|
301
|
+
it { subject.should == "6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d3860e6110c46523e" }
|
302
|
+
end
|
303
|
+
|
304
|
+
context "SHA-512" do
|
305
|
+
let(:digest) { Krypt::Digest::SHA512.new }
|
306
|
+
it { subject.should == "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58" }
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'krypt'
|
3
|
+
|
4
|
+
describe "Krypt::PBKDF2" do
|
5
|
+
|
6
|
+
let(:pb) { Krypt::PBKDF2 }
|
7
|
+
|
8
|
+
describe "#generate" do
|
9
|
+
it "returns a String with binary encoding" do
|
10
|
+
pbkdf = pb.new(Krypt::Digest::SHA1.new)
|
11
|
+
pbkdf.generate("pwd", "salt", 1, 20).encoding.should == Encoding::BINARY
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#generate_hex" do
|
16
|
+
it "returns a String with US-ASCII encoding" do
|
17
|
+
pbkdf = pb.new(Krypt::Digest::SHA1.new)
|
18
|
+
pbkdf.generate_hex("pwd", "salt", 1, 20).encoding.should == Encoding::US_ASCII
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "conforms to RFC6070 test vectors" do
|
23
|
+
let(:instance) { pb.new(Krypt::Digest::SHA1.new) }
|
24
|
+
let(:binary) { instance.generate(pwd, salt, iter, len) }
|
25
|
+
let(:hex) { instance.generate_hex(pwd, salt, iter, len) }
|
26
|
+
|
27
|
+
context "#1" do
|
28
|
+
let(:pwd) { "password" }
|
29
|
+
let(:salt) { "salt" }
|
30
|
+
let(:iter) { 1 }
|
31
|
+
let(:len) { 20 }
|
32
|
+
let(:expected) { "0c60c80f961f0e71f3a9b524af6012062fe037a6" }
|
33
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
34
|
+
it { hex.should == expected }
|
35
|
+
end
|
36
|
+
|
37
|
+
context "#2" do
|
38
|
+
let(:pwd) { "password" }
|
39
|
+
let(:salt) { "salt" }
|
40
|
+
let(:iter) { 2 }
|
41
|
+
let(:len) { 20 }
|
42
|
+
let(:expected) { "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957" }
|
43
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
44
|
+
it { hex.should == expected }
|
45
|
+
end
|
46
|
+
|
47
|
+
context "#3" do
|
48
|
+
let(:pwd) { "password" }
|
49
|
+
let(:salt) { "salt" }
|
50
|
+
let(:iter) { 4096 }
|
51
|
+
let(:len) { 20 }
|
52
|
+
let(:expected) { "4b007901b765489abead49d926f721d065a429c1" }
|
53
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
54
|
+
it { hex.should == expected }
|
55
|
+
end
|
56
|
+
|
57
|
+
# omit #4 because it takes too long
|
58
|
+
context "#5" do
|
59
|
+
let(:pwd) { "passwordPASSWORDpassword" }
|
60
|
+
let(:salt) { "saltSALTsaltSALTsaltSALTsaltSALTsalt" }
|
61
|
+
let(:iter) { 4096 }
|
62
|
+
let(:len) { 25 }
|
63
|
+
let(:expected) { "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038" }
|
64
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
65
|
+
it { hex.should == expected }
|
66
|
+
end
|
67
|
+
|
68
|
+
context "#6" do
|
69
|
+
let(:pwd) { "pass\0word" }
|
70
|
+
let(:salt) { "sa\0lt" }
|
71
|
+
let(:iter) { 4096 }
|
72
|
+
let(:len) { 16 }
|
73
|
+
let(:expected) { "56fa6aa75548099dcc37d7f03425e0c3" }
|
74
|
+
it { binary.should == Krypt::Hex.decode(expected) }
|
75
|
+
it { hex.should == expected }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'krypt'
|
3
|
+
|
4
|
+
describe "Krypt::Provider" do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
Krypt::Provider::PROVIDERS.clear
|
8
|
+
Krypt::Provider::PROVIDER_LIST.clear
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:prov) { Krypt::Provider }
|
12
|
+
|
13
|
+
describe "#register=" do
|
14
|
+
it "does not allow to register a provider twice under the same name" do
|
15
|
+
prov.register(:name, Object.new)
|
16
|
+
-> { prov.register(:name, Object.new) }.should raise_error prov::AlreadyExistsError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#by_name" do
|
21
|
+
it "returns nil if a provider with a given name does not exist" do
|
22
|
+
prov.by_name(:name).should be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
context "returns the provider that has been assigned to a given name" do
|
26
|
+
let(:instance) { Object.new }
|
27
|
+
specify do
|
28
|
+
prov.register(:name, instance)
|
29
|
+
prov.by_name(:name).should eq(instance)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#service" do
|
35
|
+
let(:provider_a) do
|
36
|
+
Class.new do
|
37
|
+
def new_service(klass, *args)
|
38
|
+
return :A if klass == Krypt::Digest || klass == String
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
end.new
|
42
|
+
end
|
43
|
+
|
44
|
+
let(:provider_b) do
|
45
|
+
Class.new do
|
46
|
+
def new_service(klass, *args)
|
47
|
+
return :B if klass == Krypt::Digest || klass == Integer
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
end.new
|
51
|
+
end
|
52
|
+
|
53
|
+
context "returns provider features based on the order they were registered" do
|
54
|
+
it "raises ServiceNotAvailableError if a requested feature is not supported by any provider" do
|
55
|
+
prov.register(:a, provider_a)
|
56
|
+
prov.register(:b, provider_b)
|
57
|
+
-> { prov.new_service(Object, "test") }.should raise_error prov::ServiceNotAvailableError
|
58
|
+
end
|
59
|
+
|
60
|
+
it "finds a service only available in a specific provider" do
|
61
|
+
prov.register(:a, provider_a)
|
62
|
+
prov.register(:b, provider_b)
|
63
|
+
prov.new_service(String).should eq(:A)
|
64
|
+
prov.new_service(Integer).should eq(:B)
|
65
|
+
end
|
66
|
+
|
67
|
+
context "returns the service of the provider registered last if the service is supported by more than one provider" do
|
68
|
+
specify "first a, then b" do
|
69
|
+
prov.register(:a, provider_a)
|
70
|
+
prov.register(:b, provider_b)
|
71
|
+
prov.new_service(Krypt::Digest).should eq(:B)
|
72
|
+
end
|
73
|
+
|
74
|
+
specify "first b, then a" do
|
75
|
+
prov.register(:b, provider_b)
|
76
|
+
prov.register(:a, provider_a)
|
77
|
+
prov.new_service(Krypt::Digest).should eq(:A)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|