relay-crypto 0.1.0

The crypto library for the Relay Ecosystem.
Documentation
use sha2::{Digest, Sha256};

/// Creates a unique key identifier (KID) from a given X25519 public key.
pub fn kid_from_x25519_pub(key: &[u8; 32]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(key);
    hex::encode(hasher.finalize())
        .to_uppercase()
        .as_bytes()
        .chunks(4)
        .map(std::str::from_utf8)
        .collect::<Result<Vec<&str>, _>>()
        .unwrap()
        .join(":")
}

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

    #[test]
    fn nil_kid_from_x25519_pub() {
        assert_eq!(
            &kid_from_x25519_pub(&[0u8; 32]),
            "6668:7AAD:F862:BD77:6C8F:C18B:8E9F:8E20:0897:1485:6EE2:33B3:902A:591D:0D5F:2925"
        );
    }
}