rs_yescrypt_hash/
lib.rs

1//! # C bindings to `Yescrypt` key derivation function
2
3#[cfg(windows)]
4#[no_mangle]
5extern "C" {
6    fn yescrypt_hash(
7        passwd: *const u8,
8        buf: *mut u8,
9    );
10}
11
12#[cfg(not(windows))]
13#[allow(unused_attributes)]
14#[no_mangle]
15extern "C" {
16    fn yescrypt_hash(
17        passwd: *const u8,
18        buf: *mut u8,
19    );
20}
21
22/// Derive fixed size key for given `salt` and `passphrase`
23///
24/// #Arguments:
25/// passwd - password to be derived
26/// output - resulting byte slice
27///
28pub fn yescrypt(passwd: &[u8], output: &mut [u8]) {
29    unsafe {
30        yescrypt_hash(
31            passwd.as_ptr(),
32            output.as_mut_ptr(),
33        );
34    }
35}
36
37pub fn get_yescrypt_hash<T: AsRef<[u8]>>(input: T) -> [u8; 32] {
38    unsafe {
39        let mut buffer = [0u8; 32];
40        yescrypt_hash(input.as_ref().as_ptr(), buffer.as_mut_ptr());
41        buffer
42    }
43}
44
45
46#[cfg(test)]
47mod tests {
48    extern crate hex;
49    use hex::{FromHex, ToHex};
50    use super::*;
51    use tests::hex::encode;
52    
53    #[test]
54    fn test_yescrypt() {
55        const PASSWD: &str = "240a5a20727df120f94999fd6e1df9a0dee583541e829597090ccaa5573b33b89f19121dbab36a503dfea48d17a160d100a78187ee80cf8ffd027bed3e82d03aa11e2d59da1cbc5ba79d011d00000a78";
56        let passwd:[u8; 80] = <[u8; 80]>::from_hex(PASSWD).expect("Decoding failed");
57
58        let mut buf = [0u8; 32];
59
60        yescrypt(&passwd, &mut buf);
61
62        assert_eq!("efae4a3fddcd185f4faddc8b41cf85f7cad91be3b525affd9ef17bfb00000000", encode(buf.as_ref()));
63    }
64
65        #[test]
66    fn test_yescrypt_hash() {
67        let passwd = "240a5a20727df120f94999fd6e1df9a0dee583541e829597090ccaa5573b33b89f19121dbab36a503dfea48d17a160d100a78187ee80cf8ffd027bed3e82d03aa11e2d59da1cbc5ba79d011d00000a78";
68        let passwd_vec = Vec::from_hex(passwd).unwrap();
69        let md = get_yescrypt_hash(passwd_vec);
70        println!("input: {}", passwd);
71        println!("output: {:?}", md.encode_hex::<String>());
72        assert_eq!(md.to_vec(), Vec::from_hex("efae4a3fddcd185f4faddc8b41cf85f7cad91be3b525affd9ef17bfb00000000").unwrap())
73    }
74}