extrapaytr-crypto 0.1.1

Digest, HMAC, constant-time comparison and at-rest sealing helpers for ExtraPayTR
Documentation
use sha1::Sha1;
use sha2::{Digest, Sha256, Sha512};

/// SHA-1 is cryptographically broken for collision resistance and should
/// never be chosen for new designs. It's exposed here only because some
/// legacy provider protocols (e.g. Garanti BBVA's GVPS `hashedPassword`
/// stage) mandate it — adapters use this to match an existing wire
/// protocol, not as a security recommendation.
pub fn sha1(input: impl AsRef<[u8]>) -> [u8; 20] {
    Sha1::digest(input).into()
}

pub fn sha256(input: impl AsRef<[u8]>) -> [u8; 32] {
    Sha256::digest(input).into()
}

pub fn sha512(input: impl AsRef<[u8]>) -> [u8; 64] {
    Sha512::digest(input).into()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sha256_empty_string_known_vector() {
        let expected =
            hex::decode("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
                .unwrap();
        assert_eq!(sha256(b"").to_vec(), expected);
    }

    #[test]
    fn sha512_empty_string_known_vector() {
        let expected = hex::decode(
            "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
        )
        .unwrap();
        assert_eq!(sha512(b"").to_vec(), expected);
    }
}