From 9562dc92b9a94df331fc326c23b4dfe3b35706b3 Mon Sep 17 00:00:00 2001 From: Tanner Davies Date: Fri, 29 Sep 2023 17:01:22 -0600 Subject: [PATCH] Add getter methods to Hash --- Cargo.lock | 193 ++++++++++++++++++++++++++++++++-------- Cargo.toml | 4 +- Readme.md | 2 +- deployment-checklist.md | 9 +- src/hasher.rs | 80 +++++++++++++---- src/lib.rs | 2 +- 6 files changed, 226 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d281cd2..438796a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,16 +4,16 @@ version = 3 [[package]] name = "aho-corasick" -version = "1.0.2" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" dependencies = [ "memchr", ] [[package]] name = "argon2-kdf" -version = "1.3.0" +version = "1.4.0" dependencies = [ "base64", "bindgen", @@ -23,15 +23,15 @@ dependencies = [ [[package]] name = "base64" -version = "0.21.2" +version = "0.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" +checksum = "9ba43ea6f343b788c8764558649e08df62f86c6ef251fdaeb1ffd010a9ae50a2" [[package]] name = "bindgen" -version = "0.66.1" +version = "0.68.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" +checksum = "726e4313eb6ec35d2730258ad4e15b547ee75d6afaa1361a922e78e59b7d8078" dependencies = [ "bitflags", "cexpr", @@ -52,17 +52,18 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.3.3" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "630be753d4e58660abd17930c71b647fe46c27ea6b63cc59e1e3851406972e42" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "cc" -version = "1.0.79" +version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ "jobserver", + "libc", ] [[package]] @@ -97,6 +98,27 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "errno" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "getrandom" version = "0.2.10" @@ -114,6 +136,15 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "home" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" +dependencies = [ + "windows-sys", +] + [[package]] name = "jobserver" version = "0.1.26" @@ -137,9 +168,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.147" +version = "0.2.148" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" [[package]] name = "libloading" @@ -151,17 +182,23 @@ dependencies = [ "winapi", ] +[[package]] +name = "linux-raw-sys" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128" + [[package]] name = "log" -version = "0.4.19" +version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" -version = "2.5.0" +version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" [[package]] name = "minimal-lexical" @@ -199,9 +236,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "prettyplease" -version = "0.2.12" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c64d9ba0963cdcea2e1b2230fbae2bab30eb25a174be395c41e764bfb65dd62" +checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d" dependencies = [ "proc-macro2", "syn", @@ -209,18 +246,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.31" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] @@ -257,9 +294,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.1" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" +checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" dependencies = [ "aho-corasick", "memchr", @@ -269,9 +306,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.3" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310" +checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" dependencies = [ "aho-corasick", "memchr", @@ -280,9 +317,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] name = "rustc-hash" @@ -290,17 +327,30 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustix" +version = "0.38.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f9da0cbd88f9f09e7814e388301c8414c51c62aa6ce1e4b5c551d49d96e531" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "shlex" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" +checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" [[package]] name = "syn" -version = "2.0.27" +version = "2.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" +checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" dependencies = [ "proc-macro2", "quote", @@ -309,9 +359,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "wasi" @@ -321,13 +371,14 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "which" -version = "4.4.0" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" dependencies = [ "either", - "libc", + "home", "once_cell", + "rustix", ] [[package]] @@ -351,3 +402,69 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index 019cded..c106ac7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "argon2-kdf" -version = "1.3.0" +version = "1.4.0" edition = "2021" readme = "Readme.md" license = "MIT" @@ -14,5 +14,5 @@ base64 = "0.21.*" rand = "0.8.*" [build-dependencies] -bindgen = "0.66.*" +bindgen = "0.68.*" cc = { version = "1.0.*", features = ["parallel"] } diff --git a/Readme.md b/Readme.md index 84618e0..2ecf1c0 100644 --- a/Readme.md +++ b/Readme.md @@ -23,7 +23,7 @@ To use argon2-kdf, add the following to your Cargo.toml: ```toml [dependencies] -argon2-kdf = "1.3.0" +argon2-kdf = "1.4.0" ``` # Examples diff --git a/deployment-checklist.md b/deployment-checklist.md index 9c49c30..b1efe91 100644 --- a/deployment-checklist.md +++ b/deployment-checklist.md @@ -4,7 +4,8 @@ 2. Run `cargo test --release` 3. Update version in `Cargo.toml` 4. Update version in `Readme.md` -5. Update examples in `Readme.md` and `lib.rs` -6. Tag a commit with the release -7. Mark a release on GitHub -8. Run `cargo publish` +5. Update version in `lib.rs` documentation comment +6. Update examples in `Readme.md` and `lib.rs` +7. Tag a commit with the release +8. Mark a release on GitHub +9. Run `cargo publish` diff --git a/src/hasher.rs b/src/hasher.rs index 5dc2ead..24a0f51 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -305,16 +305,14 @@ impl<'a> Hasher<'a> { Vec::from(s) } else { let mut rand_salt = MaybeUninit::new(Vec::with_capacity(salt_len_usize)); - let salt = unsafe { + unsafe { (*rand_salt.as_mut_ptr()).set_len(salt_len_usize); (*rand_salt.as_mut_ptr()) .try_fill(&mut OsRng) .expect("Failed to fill buffer with random bytes"); rand_salt.assume_init() - }; - - salt + } }; let (secret_ptr, secret_len) = { @@ -483,6 +481,26 @@ impl Hash { &self.salt } + /// Returns the algorithm used to generate the hash. + pub fn algorithm(&self) -> Algorithm { + self.alg + } + + /// Returns the memory cost used to generate the hash. + pub fn memory_cost_kib(&self) -> u32 { + self.mem_cost_kib + } + + /// Returns the number of iterations used to generate the hash. + pub fn iterations(&self) -> u32 { + self.iterations + } + + /// Returns the number of threads used to generate the hash. + pub fn threads(&self) -> u32 { + self.threads + } + /// Checks if the hash matches the provided password. /// /// Because verification requires re-hashing the password, this is an expensive operation. @@ -744,8 +762,8 @@ mod tests { .algorithm(Algorithm::Argon2d) .salt_length(16) .hash_length(32) - .iterations(2) - .memory_cost_kib(128) + .iterations(1) + .memory_cost_kib(16) .threads(1) .secret((&key).into()); @@ -763,8 +781,8 @@ mod tests { let hash = Hasher::default() .salt_length(16) .hash_length(32) - .iterations(2) - .memory_cost_kib(128) + .iterations(1) + .memory_cost_kib(16) .threads(1) .hash(auth_string) .unwrap() @@ -782,8 +800,8 @@ mod tests { .algorithm(Algorithm::Argon2i) .salt_length(16) .hash_length(32) - .iterations(2) - .memory_cost_kib(128) + .iterations(1) + .memory_cost_kib(16) .threads(1) .secret((&key).into()); @@ -803,8 +821,8 @@ mod tests { .algorithm(Algorithm::Argon2id) .salt_length(16) .hash_length(32) - .iterations(2) - .memory_cost_kib(128) + .iterations(1) + .memory_cost_kib(16) .threads(1) .secret((&key).into()); @@ -815,6 +833,32 @@ mod tests { .verify_with_secret(auth_string, (&key).into())); } + #[test] + fn test_get_fields() { + let auth_string = b"@Pa$$20rd-Test"; + let salt = b"seasalts"; + + let hash_builder = Hasher::new() + .algorithm(Algorithm::Argon2d) + .custom_salt(salt) + .hash_length(32) + .iterations(1) + .memory_cost_kib(16) + .threads(1); + + let hash = hash_builder.hash(auth_string).unwrap().to_string(); + let hash = Hash::from_str(&hash).unwrap(); + + assert!(hash.verify(auth_string)); + + assert!(matches!(hash.algorithm(), Algorithm::Argon2d)); + assert_eq!(hash.salt_bytes(), salt); + assert_eq!(hash.as_bytes().len(), 32); + assert_eq!(hash.iterations(), 1); + assert_eq!(hash.memory_cost_kib(), 16); + assert_eq!(hash.threads(), 1); + } + #[test] fn test_custom_salt() { let auth_string = b"@Pa$$20rd-Test"; @@ -840,8 +884,8 @@ mod tests { let hash_builder = Hasher::default() .salt_length(16) .hash_length(32) - .iterations(2) - .memory_cost_kib(128) + .iterations(1) + .memory_cost_kib(16) .threads(1) .secret((&key).into()); @@ -860,8 +904,8 @@ mod tests { let hash_builder = Hasher::default() .salt_length(16) .hash_length(32) - .iterations(2) - .memory_cost_kib(128) + .iterations(1) + .memory_cost_kib(16) .threads(1) .secret((&key).into()); @@ -880,8 +924,8 @@ mod tests { let hash_builder = Hasher::default() .salt_length(16) .hash_length(32) - .iterations(2) - .memory_cost_kib(128) + .iterations(1) + .memory_cost_kib(16) .threads(1) .secret((&key).into()); diff --git a/src/lib.rs b/src/lib.rs index 2d96af5..2f0a853 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ //! //! ```toml //! [dependencies] -//! argon2-kdf = "1.3.0" +//! argon2-kdf = "1.4.0" //! ``` //! //! # Examples