Created
May 10, 2018 11:53
Serde error while enum flattening
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)] | |
pub struct KdfParams { | |
/// Key derivation function | |
#[serde(flatten)] | |
pub kdf: Kdf, | |
/// `Kdf` length for parameters | |
pub dklen: usize, | |
/// Cryptographic salt for `Kdf` | |
pub salt: Salt, | |
} | |
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)] | |
#[serde(untagged)] | |
pub enum Kdf { | |
/// PBKDF2 (not recommended, specified in (RFC 2898)[https://tools.ietf.org/html/rfc2898]) | |
#[serde(rename = "pbkdf2")] | |
Pbkdf2 { | |
/// Pseudo-Random Functions (`HMAC-SHA-256` by default) | |
prf: Prf, | |
/// Number of iterations (`262144` by default) | |
c: u32, | |
}, | |
/// Scrypt (by default, specified in (RPC 7914)[https://tools.ietf.org/html/rfc7914]) | |
#[serde(rename = "scrypt")] | |
Scrypt { | |
/// Number of iterations (`19201` by default) | |
n: u32, | |
/// Block size for the underlying hash (`8` by default) | |
r: u32, | |
/// Parallelization factor (`1` by default) | |
p: u32, | |
}, | |
} | |
#[cfg(test)] | |
mod tests { | |
const KDF_PARAMS_PBKDF2: &'static str = r#"{ | |
"c": 10240, | |
"dklen": 32, | |
"prf": "hmac-sha256", | |
"salt": "095a4028fa2474bb2191f9fc1d876c79a9ff76ed029aa7150d37da785a00175b" | |
}"#; | |
#[test] | |
fn should_serialize_kdf_params() { | |
let exp = KdfParams { | |
kdf: Kdf::Pbkdf2 { | |
prf: Prf::default(), | |
c: 10240, | |
}, | |
dklen: 32, | |
salt: serde_json::from_str( | |
"\"095a4028fa2474bb2191f9fc1d876c79a9ff76ed029aa7150d37da785a00175b\"", | |
).unwrap(), | |
}; | |
let mut act: KdfParams = serde_json::from_str(KDF_PARAMS_PBKDF2).unwrap(); | |
act = serde_json::from_str::<KdfParams>(&serde_json::to_string(&act).unwrap()).unwrap(); | |
assert_eq!(act, exp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment