sodium.cr/spec/sodium/pwhash_spec.cr

51 lines
1.3 KiB
Crystal
Raw Normal View History

2019-05-29 01:15:13 +02:00
require "../spec_helper"
2019-06-27 17:03:33 +02:00
private def pw_min
pwhash = Sodium::Pwhash.new
2019-05-29 01:15:13 +02:00
2019-06-28 02:20:02 +02:00
# set to minimum to speed up tests
pwhash.memlimit = Sodium::Pwhash::MEMLIMIT_MIN
pwhash.opslimit = Sodium::Pwhash::OPSLIMIT_MIN
2019-06-28 02:20:02 +02:00
pwhash
2019-06-27 17:03:33 +02:00
end
describe Sodium::Pwhash do
2019-06-27 17:03:33 +02:00
it "hashes and verifies a password" do
pwhash = pw_min
2019-05-29 01:15:13 +02:00
pass = "1234"
2019-06-27 17:03:33 +02:00
hash = pwhash.store pass
2019-05-29 01:15:13 +02:00
pwhash.verify hash, pass
expect_raises(Sodium::Pwhash::PasswordVerifyError) do
2019-05-29 01:15:13 +02:00
pwhash.verify hash, "5678"
end
pwhash.needs_rehash?(hash).should be_false
pwhash.opslimit = Sodium::Pwhash::OPSLIMIT_MAX
2019-05-29 01:15:13 +02:00
pwhash.needs_rehash?(hash).should be_true
end
2019-06-27 17:03:33 +02:00
it "key_derive fails without an algorithm" do
pwhash = pw_min
expect_raises(ArgumentError) do
pwhash.key_derive pwhash.salt, "foo", 16
end
end
it "derives a key from a password" do
pwhash = pw_min
pwhash.algorithm = Sodium::Pwhash::Algorithm::Argon2id13
2019-06-27 17:03:33 +02:00
salt = pwhash.salt
key1 = pwhash.key_derive salt, "foo", 16
key2 = pwhash.key_derive salt, "foo", 16
key3 = pwhash.key_derive salt, "bar", 16
key4 = pwhash.key_derive pwhash.salt, "foo", 16
key1.bytesize.should eq 16
key1.should eq key2
key1.should_not eq key3
key1.should_not eq key4
2019-06-28 02:20:02 +02:00
# BUG: validate against known passwords
2019-06-27 17:03:33 +02:00
end
2019-05-29 01:15:13 +02:00
end