Skip to main content

decode_phc

Function decode_phc 

Source
pub fn decode_phc(encoded: &str) -> Result<Decoded, Error>
Expand description

Decode a PHC string, detecting the algorithm from the $argon2* prefix.

Unlike decode_string (C decode_string, fixed $m=,t=,p=), this accepts:

  • any order of m, t, p
  • an optional data= associated-data field (node-argon2 / @phc/format)
  • unknown keys such as keyid (ignored)

$v= is still optional and still defaults to version 16.

The C-style encoder (encode_string, crate::Argon2::hash_encoded) never writes data=. This decoder exists so a verifier can still honour strings other producers emit.

use argon2_rust::{Algorithm, decode_phc};

// node-argon2 / `@phc/format` write `m,p,t`.
let d = decode_phc(
    "$argon2id$v=19$m=64,p=1,t=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70",
)?;
assert_eq!(d.algorithm, Algorithm::Argon2id);
assert_eq!(d.params.memory_kib(), 64);
assert_eq!(d.params.passes(), 1);
assert_eq!(d.salt, b"somesalt");
assert!(d.ad.is_empty());

ยงErrors

Error::DecodingFail for a malformed string, or whatever crate::params::validate_inputs returns.