Skip to content

Commit

Permalink
Tests for PBE
Browse files Browse the repository at this point in the history
  • Loading branch information
aldosolorzano committed Nov 13, 2018
1 parent e935fa9 commit abd80c4
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
61 changes: 61 additions & 0 deletions spec/crypto/PBE_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
describe Crypto::PBE do
pbe_fixture = JSON.parse(File.read('spec/fixtures/pbe.json'), symbolize_names: true)
let(:alpha_num) { ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a }
let(:specials) { ['-', '_', '+', '=', '#', '&', '*', '.'] }
let(:valid_chars) { alpha_num + specials }

describe "#PBE good" do
pbe_fixture[:valid].each do | v |
describe "#{v.slice(:key, :salt, :keylen, :iterations)}" do
let(:pbe) { Crypto::PBE.new(v[:iterations]) }
let(:key) { pbe.derived_key(v[:key], v[:salt], v[:keylen]) }
it 'should return a strong key' do
expect(key.to_hex).to eq(v[:result])
end
end
end

describe "Generate random keys" do
let(:keys) { Set.new }
5.times do
key = Crypto::PBE.generate.to_hex
it "should be unique #{key}" do
expect(keys.include?(key)).to be false
keys.add(key)
end
end
end

describe "Random password + size" do
let(:passwords) { Set.new }
key_lens = [32, 64, 40]
5.times do
size = key_lens.sample
pass = Crypto::PBE.new.random_password(size)
it "should be unique #{pass}" do
expect(passwords.include?(pass)).to be false
passwords.add(pass)
end
it "should be size #{size}" do
expect(pass.length).to be size
end
it "should contain specified chars" do
pass_chars = pass.chars
expect(pass_chars).to eq(pass_chars & valid_chars)
end
end
end
end

describe '#PBE bad' do
pbe_fixture[:invalid].each do | v |
describe "#{v[:description]}" do
let(:pbe){ Crypto::PBE.new(v[:iterations]) }
let(:error){ "integer #{v[:keylen]} too big to convert to `int'" }
it 'should raise key length error' do
expect{ pbe.derived_key(v[:key], v[:salt], v[:keylen]) }.to raise_error(RangeError, error)
end
end
end
end
end
68 changes: 68 additions & 0 deletions spec/fixtures/pbe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"valid": [
{
"key": "passwd",
"salt": "salt",
"iterations": 1,
"keylen": 64,
"result": "55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc49ca9cccf179b645991664b39d77ef317c71b845b1e30bd509112041d3a19783" },
{
"key": "Password",
"salt": "NaCl",
"iterations": 80000,
"keylen": 64,
"result": "4ddcd8f60b98be21830cee5ef22701f9641a4418d04c0414aeff08876b34ab56a1d425a1225833549adb841b51c9b3176a272bdebba1d078478f62b397f33c8d" },
{
"key": "password",
"salt": "salt",
"iterations": 1,
"keylen": 32,
"result": "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b" },
{
"key": "password",
"salt": "salt",
"iterations": 2,
"keylen": 32,
"result": "ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43" },
{
"key": "password",
"salt": "salt",
"iterations": 4096,
"keylen": 32,
"result": "c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a"
},
{
"key": "passwordPASSWORDpassword",
"salt": "saltSALTsaltSALTsaltSALTsaltSALTsalt",
"iterations": 4096,
"keylen": 40,
"result": "348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9"
},
{
"key": "",
"salt": "salt",
"iterations": 1024,
"keylen": 32,
"result": "9e83f279c040f2a11aa4a02b24c418f2d3cb39560c9627fa4f47e3bcc2897c3d"

},
{
"key": "password",
"salt": "",
"iterations": 1024,
"keylen": 32,
"result": "ea5808411eb0c7e830deab55096cee582761e22a9bc034e3ece925225b07bf46"

}
],
"invalid": [
{
"key": "password",
"salt": "afas",
"iterations": 1024,
"keylen": 35184372088832,
"result": "",
"description": "key length too long"
}
]
}

0 comments on commit abd80c4

Please sign in to comment.