crypto-async-rs 0.1.3

High-performance pure Rust cryptographic library with async streaming support
Documentation
use crate::sha512::{encode_core, get_final_chunks_sha2, process_chunk};
use futures::io::{AsyncRead, AsyncReadExt};

const H: [u64; 8] = [
    0xcbbb9d5dc1059ed8,
    0x629a292a367cd507,
    0x9159015a3070dd17,
    0x152fecd8f70e5939,
    0x67332667ffc00b31,
    0x8eb44a8768581511,
    0xdb0c2e0d64f98fa7,
    0x47b5481dbefa4fa4,
];

pub fn encode(bytes: &[u8]) -> [u8; 48] {
    let h: [u64; 8] = encode_core(bytes, H);
    let mut hh: [u8; 48] = [0; 48];
    for i in 0..6 {
        let start_index = i << 3;
        hh[start_index..start_index + 8].copy_from_slice(&h[i].to_be_bytes());
    }

    hh
}


pub async fn encode_async<R>(mut reader: R) -> Result<[u8; 48], std::io::Error>
where
    R: AsyncRead + Unpin,
{
    let mut h = H;
    let mut buffer = [0u8; 128];
    let mut buffer_len = 0;
    let mut total_length = 0;

    loop {
        let bytes_read = reader.read(&mut buffer[buffer_len..]).await?;
        if bytes_read == 0 {
            break;
        }
        
        buffer_len += bytes_read;
        total_length += bytes_read;

        if buffer_len == 128 {
            let result = process_chunk(&buffer[..buffer_len], &h);
            for i in 0..8 {
                h[i] = h[i].wrapping_add(result[i]);
            }
            buffer_len = 0;
        }
    }

    let final_chunk2 = 
        get_final_chunks_sha2(&mut buffer, total_length, buffer_len);

    let result = process_chunk(&buffer, &h);
    for i in 0..8 {
        h[i] = h[i].wrapping_add(result[i]);
    }
    if let Some(chunk2) = final_chunk2 {
        let result = process_chunk(&chunk2, &h);
        for i in 0..8 {
            h[i] = h[i].wrapping_add(result[i]);
        }
    }

    let mut hh = [0u8; 48];
    for i in 0..6 {
        let start_index = i << 3;
        hh[start_index..start_index + 8].copy_from_slice(&h[i].to_be_bytes());
    }

    Ok(hh)
}




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

    #[test]
    fn test_encode_empty_string() {
        let result = encode("".as_bytes());
        assert_eq!(
            result,
            [
                0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1,
                0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf,
                0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a,
                0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b
            ]
        );
    }

    #[test]
    fn test_encode_abc() {
        let result = encode("abc".as_bytes());
        assert_eq!(
            result,
            [
                0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6,
                0x50, 0x07, 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a,
                0x43, 0xff, 0x5b, 0xed, 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba,
                0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
            ]
        );
    }

    #[test]
    fn test_encode_message_digest() {
        let result = encode("message digest".as_bytes());
        assert_eq!(
            result,
            [
                0x47, 0x3e, 0xd3, 0x51, 0x67, 0xec, 0x1f, 0x5d, 0x8e, 0x55, 0x03, 0x68, 0xa3, 0xdb,
                0x39, 0xbe, 0x54, 0x63, 0x9f, 0x82, 0x88, 0x68, 0xe9, 0x45, 0x4c, 0x23, 0x9f, 0xc8,
                0xb5, 0x2e, 0x3c, 0x61, 0xdb, 0xd0, 0xd8, 0xb4, 0xde, 0x13, 0x90, 0xc2, 0x56, 0xdc,
                0xbb, 0x5d, 0x5f, 0xd9, 0x9c, 0xd5
            ]
        );
    }

    #[test]
    fn test_encode_abcdefghijklmnopqrstuvwxyz() {
        let result = encode("abcdefghijklmnopqrstuvwxyz".as_bytes());
        assert_eq!(
            result,
            [
                0xfe, 0xb6, 0x73, 0x49, 0xdf, 0x3d, 0xb6, 0xf5, 0x92, 0x48, 0x15, 0xd6, 0xc3, 0xdc,
                0x13, 0x3f, 0x09, 0x18, 0x09, 0x21, 0x37, 0x31, 0xfe, 0x5c, 0x7b, 0x5f, 0x49, 0x99,
                0xe4, 0x63, 0x47, 0x9f, 0xf2, 0x87, 0x7f, 0x5f, 0x29, 0x36, 0xfa, 0x63, 0xbb, 0x43,
                0x78, 0x4b, 0x12, 0xf3, 0xeb, 0xb4
            ]
        );
    }

    #[test]
    fn test_encode_alphanumeric_string() {
        let result = encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".as_bytes());
        assert_eq!(
            result,
            [
                0x17, 0x61, 0x33, 0x6e, 0x3f, 0x7c, 0xbf, 0xe5, 0x1d, 0xeb, 0x13, 0x7f, 0x02, 0x6f,
                0x89, 0xe0, 0x1a, 0x44, 0x8e, 0x3b, 0x1f, 0xaf, 0xa6, 0x40, 0x39, 0xc1, 0x46, 0x4e,
                0xe8, 0x73, 0x2f, 0x11, 0xa5, 0x34, 0x1a, 0x6f, 0x41, 0xe0, 0xc2, 0x02, 0x29, 0x47,
                0x36, 0xed, 0x64, 0xdb, 0x1a, 0x84
            ]
        );
    }

    #[test]
    fn test_encode_12345() {
        let result = encode("12345".as_bytes());
        assert_eq!(
            result,
            [
                0x0f, 0xa7, 0x69, 0x55, 0xab, 0xfa, 0x9d, 0xaf, 0xd8, 0x3f, 0xac, 0xca, 0x83, 0x43,
                0xa9, 0x2a, 0xa0, 0x94, 0x97, 0xf9, 0x81, 0x01, 0x08, 0x66, 0x11, 0xb0, 0xbf, 0xa9,
                0x5d, 0xbc, 0x0d, 0xcc, 0x66, 0x1d, 0x62, 0xe9, 0x56, 0x8a, 0x5a, 0x03, 0x2b, 0xa8,
                0x19, 0x60, 0xf3, 0xe5, 0x5d, 0x4a
            ]
        );
    }

    #[test]
    fn test_encode_long_string() {
        let result = encode(b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
        assert_eq!(
            result,
            [
                0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd,
                0x1b, 0x47, 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2, 0x2f, 0xa0, 0x80, 0x86,
                0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3,
                0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
            ]
        );
    }

    #[test]
    fn test_encode_million_a() {
        let result = encode(&vec!["a".as_bytes()[0]; 1000000]);
        assert_eq!(
            result,
            [
                0x9d, 0x0e, 0x18, 0x09, 0x71, 0x64, 0x74, 0xcb, 0x08, 0x6e, 0x83, 0x4e, 0x31, 0x0a,
                0x4a, 0x1c, 0xed, 0x14, 0x9e, 0x9c, 0x00, 0xf2, 0x48, 0x52, 0x79, 0x72, 0xce, 0xc5,
                0x70, 0x4c, 0x2a, 0x5b, 0x07, 0xb8, 0xb3, 0xdc, 0x38, 0xec, 0xc4, 0xeb, 0xae, 0x97,
                0xdd, 0xd8, 0x7f, 0x3d, 0x89, 0x85
            ]
        );
    }

    #[test]
    fn test_encode_repeated_pattern() {
        let result = encode(
            vec!["01234567012345670123456701234567"; 20]
                .join("")
                .as_bytes(),
        );
        assert_eq!(
            result,
            [
                0x2f, 0xc6, 0x4a, 0x4f, 0x50, 0x0d, 0xdb, 0x68, 0x28, 0xf6, 0xa3, 0x43, 0x0b, 0x8d,
                0xd7, 0x2a, 0x36, 0x8e, 0xb7, 0xf3, 0xa8, 0x32, 0x2a, 0x70, 0xbc, 0x84, 0x27, 0x5b,
                0x9c, 0x0b, 0x3a, 0xb0, 0x0d, 0x27, 0xa5, 0xcc, 0x3c, 0x2d, 0x22, 0x4a, 0xa6, 0xb6,
                0x1a, 0x0d, 0x79, 0xfb, 0x45, 0x96
            ]
        );
    }

    #[test]
    fn test_encode_single_byte_b9() {
        let result = encode(&[0xb9]);
        assert_eq!(
            result,
            [
                0xbc, 0x80, 0x89, 0xa1, 0x90, 0x07, 0xc0, 0xb1, 0x41, 0x95, 0xf4, 0xec, 0xc7, 0x40,
                0x94, 0xfe, 0xc6, 0x4f, 0x01, 0xf9, 0x09, 0x29, 0x28, 0x2c, 0x2f, 0xb3, 0x92, 0x88,
                0x15, 0x78, 0x20, 0x8a, 0xd4, 0x66, 0x82, 0x8b, 0x1c, 0x6c, 0x28, 0x3d, 0x27, 0x22,
                0xcf, 0x0a, 0xd1, 0xab, 0x69, 0x38
            ]
        );
    }

    #[test]
    fn test_encode_16_byte_array() {
        let result = encode(&[
            0xa4, 0x1c, 0x49, 0x77, 0x79, 0xc0, 0x37, 0x5f, 0xf1, 0x0a, 0x7f, 0x4e, 0x08, 0x59,
            0x17, 0x39,
        ]);
        assert_eq!(
            result,
            [
                0xc9, 0xa6, 0x84, 0x43, 0xa0, 0x05, 0x81, 0x22, 0x56, 0xb8, 0xec, 0x76, 0xb0, 0x05,
                0x16, 0xf0, 0xdb, 0xb7, 0x4f, 0xab, 0x26, 0xd6, 0x65, 0x91, 0x3f, 0x19, 0x4b, 0x6f,
                0xfb, 0x0e, 0x91, 0xea, 0x99, 0x67, 0x56, 0x6b, 0x58, 0x10, 0x9c, 0xbc, 0x67, 0x5c,
                0xc2, 0x08, 0xe4, 0xc8, 0x23, 0xf7
            ]
        );
    }

    #[test]
    fn test_encode_large_byte_array() {
        let result = encode(&[
            0x39, 0x96, 0x69, 0xe2, 0x8f, 0x6b, 0x9c, 0x6d, 0xbc, 0xbb, 0x69, 0x12, 0xec, 0x10,
            0xff, 0xcf, 0x74, 0x79, 0x03, 0x49, 0xb7, 0xdc, 0x8f, 0xbe, 0x4a, 0x8e, 0x7b, 0x3b,
            0x56, 0x21, 0xdb, 0x0f, 0x3e, 0x7d, 0xc8, 0x7f, 0x82, 0x32, 0x64, 0xbb, 0xe4, 0x0d,
            0x18, 0x11, 0xc9, 0xea, 0x20, 0x61, 0xe1, 0xc8, 0x4a, 0xd1, 0x0a, 0x23, 0xfa, 0xc1,
            0x72, 0x7e, 0x72, 0x02, 0xfc, 0x3f, 0x50, 0x42, 0xe6, 0xbf, 0x58, 0xcb, 0xa8, 0xa2,
            0x74, 0x6e, 0x1f, 0x64, 0xf9, 0xb9, 0xea, 0x35, 0x2c, 0x71, 0x15, 0x07, 0x05, 0x3c,
            0xf4, 0xe5, 0x33, 0x9d, 0x52, 0x86, 0x5f, 0x25, 0xcc, 0x22, 0xb5, 0xe8, 0x77, 0x84,
            0xa1, 0x2f, 0xc9, 0x61, 0xd6, 0x6c, 0xb6, 0xe8, 0x95, 0x73, 0x19, 0x9a, 0x2c, 0xe6,
            0x56, 0x5c, 0xbd, 0xf1, 0x3d, 0xca, 0x40, 0x38, 0x32, 0xcf, 0xcb, 0x0e, 0x8b, 0x72,
            0x11, 0xe8, 0x3a, 0xf3, 0x2a, 0x11, 0xac, 0x17, 0x92, 0x9f, 0xf1, 0xc0, 0x73, 0xa5,
            0x1c, 0xc0, 0x27, 0xaa, 0xed, 0xef, 0xf8, 0x5a, 0xad, 0x7c, 0x2b, 0x7c, 0x5a, 0x80,
            0x3e, 0x24, 0x04, 0xd9, 0x6d, 0x2a, 0x77, 0x35, 0x7b, 0xda, 0x1a, 0x6d, 0xae, 0xed,
            0x17, 0x15, 0x1c, 0xb9, 0xbc, 0x51, 0x25, 0xa4, 0x22, 0xe9, 0x41, 0xde, 0x0c, 0xa0,
            0xfc, 0x50, 0x11, 0xc2, 0x3e, 0xcf, 0xfe, 0xfd, 0xd0, 0x96, 0x76, 0x71, 0x1c, 0xf3,
            0xdb, 0x0a, 0x34, 0x40, 0x72, 0x0e, 0x16, 0x15, 0xc1, 0xf2, 0x2f, 0xbc, 0x3c, 0x72,
            0x1d, 0xe5, 0x21, 0xe1, 0xb9, 0x9b, 0xa1, 0xbd, 0x55, 0x77, 0x40, 0x86, 0x42, 0x14,
            0x7e, 0xd0, 0x96,
        ]);
        assert_eq!(
            result,
            [
                0x4f, 0x44, 0x0d, 0xb1, 0xe6, 0xed, 0xd2, 0x89, 0x9f, 0xa3, 0x35, 0xf0, 0x95, 0x15,
                0xaa, 0x02, 0x5e, 0xe1, 0x77, 0xa7, 0x9f, 0x4b, 0x4a, 0xaf, 0x38, 0xe4, 0x2b, 0x5c,
                0x4d, 0xe6, 0x60, 0xf5, 0xde, 0x8f, 0xb2, 0xa5, 0xb2, 0xfb, 0xd2, 0xa3, 0xcb, 0xff,
                0xd2, 0x0c, 0xff, 0x12, 0x88, 0xc0
            ]
        );
    }

    #[tokio::test]
    async fn test_encode_async() {
        let data = "The quick brown fox jumps over the lazy dog".as_bytes();
        let async_result = encode_async(data).await.unwrap();
        let sync_result = encode(data);
        assert_eq!(async_result, sync_result);
    }

}