-
Notifications
You must be signed in to change notification settings - Fork 385
/
sorcery_crypto_providers_spec.rb
198 lines (147 loc) · 6.46 KB
/
sorcery_crypto_providers_spec.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
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
require 'spec_helper'
describe "Crypto Providers wrappers" do
describe Sorcery::CryptoProviders::MD5 do
after(:each) do
Sorcery::CryptoProviders::MD5.reset!
end
it "encrypt works via wrapper like normal lib" do
expect(Sorcery::CryptoProviders::MD5.encrypt 'Noam Ben-Ari').to eq Digest::MD5.hexdigest('Noam Ben-Ari')
end
it "works with multiple stretches" do
Sorcery::CryptoProviders::MD5.stretches = 3
expect(Sorcery::CryptoProviders::MD5.encrypt 'Noam Ben-Ari').to eq Digest::MD5.hexdigest(Digest::MD5.hexdigest(Digest::MD5.hexdigest('Noam Ben-Ari')))
end
it "matches? returns true when matches" do
expect(Sorcery::CryptoProviders::MD5.matches? Digest::MD5.hexdigest('Noam Ben-Ari'), 'Noam Ben-Ari').to be true
end
it "matches? returns false when no match" do
expect(Sorcery::CryptoProviders::MD5.matches? Digest::MD5.hexdigest('Noam Ben-Ari'), 'Some Dude').to be false
end
end
describe Sorcery::CryptoProviders::SHA1 do
before(:all) do
@digest = 'Noam Ben-Ari'
Sorcery::CryptoProviders::SHA1.stretches.times {@digest = Digest::SHA1.hexdigest(@digest)}
end
after(:each) do
Sorcery::CryptoProviders::SHA1.reset!
end
it "encrypt works via wrapper like normal lib" do
expect(Sorcery::CryptoProviders::SHA1.encrypt 'Noam Ben-Ari').to eq @digest
end
it "works with multiple stretches" do
Sorcery::CryptoProviders::SHA1.stretches = 3
expect(Sorcery::CryptoProviders::SHA1.encrypt 'Noam Ben-Ari').to eq Digest::SHA1.hexdigest(Digest::SHA1.hexdigest(Digest::SHA1.hexdigest('Noam Ben-Ari')))
end
it "matches? returns true when matches" do
expect(Sorcery::CryptoProviders::SHA1.matches? @digest, 'Noam Ben-Ari').to be true
end
it "matches? returns false when no match" do
expect(Sorcery::CryptoProviders::SHA1.matches? @digest, 'Some Dude').to be false
end
it "matches password encrypted using salt and join token from upstream" do
Sorcery::CryptoProviders::SHA1.join_token = "test"
expect(Sorcery::CryptoProviders::SHA1.encrypt ['password', 'gq18WBnJYNh2arkC1kgH']).to eq '894b5bf1643b8d0e1b2eaddb22426be7036dab70'
end
end
describe Sorcery::CryptoProviders::SHA256 do
before(:all) do
@digest = 'Noam Ben-Ari'
Sorcery::CryptoProviders::SHA256.stretches.times {@digest = Digest::SHA256.hexdigest(@digest)}
end
after(:each) do
Sorcery::CryptoProviders::SHA256.reset!
end
it "encrypt works via wrapper like normal lib" do
expect(Sorcery::CryptoProviders::SHA256.encrypt 'Noam Ben-Ari').to eq @digest
end
it "works with multiple stretches" do
Sorcery::CryptoProviders::SHA256.stretches = 3
expect(Sorcery::CryptoProviders::SHA256.encrypt 'Noam Ben-Ari').to eq Digest::SHA256.hexdigest(Digest::SHA256.hexdigest(Digest::SHA256.hexdigest('Noam Ben-Ari')))
end
it "matches? returns true when matches" do
expect(Sorcery::CryptoProviders::SHA256.matches? @digest, 'Noam Ben-Ari').to be true
end
it "matches? returns false when no match" do
expect(Sorcery::CryptoProviders::SHA256.matches? @digest, 'Some Dude').to be false
end
end
describe Sorcery::CryptoProviders::SHA512 do
before(:all) do
@digest = 'Noam Ben-Ari'
Sorcery::CryptoProviders::SHA512.stretches.times {@digest = Digest::SHA512.hexdigest(@digest)}
end
after(:each) do
Sorcery::CryptoProviders::SHA512.reset!
end
it "encrypt works via wrapper like normal lib" do
expect(Sorcery::CryptoProviders::SHA512.encrypt 'Noam Ben-Ari').to eq @digest
end
it "works with multiple stretches" do
Sorcery::CryptoProviders::SHA512.stretches = 3
expect(Sorcery::CryptoProviders::SHA512.encrypt 'Noam Ben-Ari').to eq Digest::SHA512.hexdigest(Digest::SHA512.hexdigest(Digest::SHA512.hexdigest('Noam Ben-Ari')))
end
it "matches? returns true when matches" do
expect(Sorcery::CryptoProviders::SHA512.matches? @digest, 'Noam Ben-Ari').to be true
end
it "matches? returns false when no match" do
expect(Sorcery::CryptoProviders::SHA512.matches? @digest, 'Some Dude').to be false
end
end
describe Sorcery::CryptoProviders::AES256 do
before(:all) do
aes = OpenSSL::Cipher::Cipher.new("AES-256-ECB")
aes.encrypt
@key = "asd234dfs423fddsmndsflktsdf32343"
aes.key = @key
@digest = 'Noam Ben-Ari'
@digest = [aes.update(@digest) + aes.final].pack("m").chomp
Sorcery::CryptoProviders::AES256.key = @key
end
it "encrypt works via wrapper like normal lib" do
expect(Sorcery::CryptoProviders::AES256.encrypt 'Noam Ben-Ari').to eq @digest
end
it "matches? returns true when matches" do
expect(Sorcery::CryptoProviders::AES256.matches? @digest, 'Noam Ben-Ari').to be true
end
it "matches? returns false when no match" do
expect(Sorcery::CryptoProviders::AES256.matches? @digest, 'Some Dude').to be false
end
it "can be decrypted" do
aes = OpenSSL::Cipher::Cipher.new("AES-256-ECB")
aes.decrypt
aes.key = @key
expect(aes.update(@digest.unpack("m").first) + aes.final).to eq "Noam Ben-Ari"
end
end
describe Sorcery::CryptoProviders::BCrypt do
before(:all) do
Sorcery::CryptoProviders::BCrypt.cost = 1
@digest = BCrypt::Password.create('Noam Ben-Ari', :cost => Sorcery::CryptoProviders::BCrypt.cost)
end
after(:each) do
Sorcery::CryptoProviders::BCrypt.reset!
end
it "is comparable with original secret" do
expect(BCrypt::Password.new Sorcery::CryptoProviders::BCrypt.encrypt('Noam Ben-Ari')).to eq 'Noam Ben-Ari'
end
it "works with multiple costs" do
Sorcery::CryptoProviders::BCrypt.cost = 3
expect(BCrypt::Password.new(Sorcery::CryptoProviders::BCrypt.encrypt 'Noam Ben-Ari')).to eq 'Noam Ben-Ari'
end
it "matches? returns true when matches" do
expect(Sorcery::CryptoProviders::BCrypt.matches? @digest, 'Noam Ben-Ari').to be true
end
it "matches? returns false when no match" do
expect(Sorcery::CryptoProviders::BCrypt.matches? @digest, 'Some Dude').to be false
end
it "respond_to?(:stretches) returns true" do
expect(Sorcery::CryptoProviders::BCrypt.respond_to? :stretches).to be true
end
it "sets cost when stretches is set" do
Sorcery::CryptoProviders::BCrypt.stretches = 4
# stubbed in Sorcery::TestHelpers::Internal
expect(Sorcery::CryptoProviders::BCrypt.cost).to eq 1
end
end
end