oxedyne/fe2o3/fe2o3_hash/tests/password.rs
2.0 KiB, 1 run
created by r1870400018:74077, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_hash::kdf::{ |
| 2 | hash_password, |
| 3 | verify_password, |
| 4 | PASSWORD_MEM_COST, |
| 5 | PASSWORD_TIME_COST, |
| 6 | }; |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | |
| 10 | |
| 11 | #[test] |
| 12 | fn password_round_trip_and_wrong_password() -> Outcome<()> { |
| 13 | let phc = res!(hash_password(b"correct horse battery")); |
| 14 | let want = fmt!("$argon2id$v=19$m={},t={},p=1$", PASSWORD_MEM_COST, PASSWORD_TIME_COST); |
| 15 | req!(phc.starts_with(&want), true, "The PHC string '{}' does not start '{}'.", phc, want); |
| 16 | req!(phc.contains("correct"), false); |
| 17 | req!(res!(verify_password(b"correct horse battery", &phc)), true); |
| 18 | req!(res!(verify_password(b"correct horse batterz", &phc)), false); |
| 19 | req!(res!(verify_password(b"", &phc)), false); |
| 20 | Ok(()) |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn password_salts_differ() -> Outcome<()> { |
| 25 | let a = res!(hash_password(b"same password")); |
| 26 | let b = res!(hash_password(b"same password")); |
| 27 | req!((a == b), false, "Two hashes of one password share a salt."); |
| 28 | req!(res!(verify_password(b"same password", &b)), true); |
| 29 | Ok(()) |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | fn password_verify_reads_a_foreign_weaker_string() -> Outcome<()> { |
| 34 | // RFC 9106-style parameters made elsewhere (rust-argon2's own test vector, Argon2i, p=4). |
| 35 | let phc = "$argon2i$v=19$m=4096,t=3,p=4$YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo$BvBk2OaSofBHfbrUW61nHrWB/43xgfs/QJJ5DkMAd8I"; |
| 36 | req!(res!(verify_password(b"foo", phc)), true); |
| 37 | req!(res!(verify_password(b"bar", phc)), false); |
| 38 | Ok(()) |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn password_verify_refuses_bad_strings() -> Outcome<()> { |
| 43 | // A stored string asking 4 GiB is refused, not run. |
| 44 | let big = "$argon2id$v=19$m=4194304,t=2,p=1$YWJjZGVmZ2hpamtsbW5vcA$BvBk2OaSofBHfbrUW61nHrWB/43xgfs/QJJ5DkMAd8I"; |
| 45 | req!(verify_password(b"x", big).is_err(), true); |
| 46 | // A four byte salt. |
| 47 | let short = "$argon2id$v=19$m=4096,t=2,p=1$YWJjZA$BvBk2OaSofBHfbrUW61nHrWB/43xgfs/QJJ5DkMAd8I"; |
| 48 | req!(verify_password(b"x", short).is_err(), true); |
| 49 | req!(verify_password(b"x", "").is_err(), true); |
| 50 | req!(verify_password(b"x", "not a hash").is_err(), true); |
| 51 | Ok(()) |
| 52 | } |