#openssl #symmetric #async #stream #symm

async_symm_crypto

An async openssl symmetric cryptography

3 unstable releases

0.2.0 Jan 19, 2025
0.1.1 Jan 8, 2025
0.1.0 Jan 8, 2025

#824 in Cryptography

Download history 260/week @ 2025-01-08 114/week @ 2025-01-15 3/week @ 2025-01-22 4/week @ 2025-01-29 9/week @ 2025-02-05

132 downloads per month
Used in flutterwave-v3

MIT license

24KB
210 lines

Async-Symm-Crypto

A crate that wraps around openssl providing very convenient async interfaces to openssl's symmetric cryptography functions. It is:

Crates.io Crates.io Docs Build MIT licensed

Website | API Docs

Example

Basic example of stream cryptography

Make sure openssl is installed on the operating

[dependencies]
async_symm_crypto = "0.2.0"

Then, on your main.rs:

use async_symm_crypto::AsyncEncryption;
use openssl;
use futures::StreamExt;
use std::ops::Deref;
use std::pin::Pin;
use tokio; 

static TEST_STRING:&'static str = "Cryptographic protocols like TLS, SSH, IPsec, and OpenVPN commonly use block cipher algorithms, such as AES, Triple-DES, and Blowfish, to encrypt data between clients and servers. To use such algorithms, the data is broken into fixed-length chunks, called blocks, and each block is encrypted separately according to a mode of operation. Older block ciphers, such as Triple-DES and Blowfish use a block size of 64 bits, whereas AES uses a block size of 128 bits.";
static TEST_KEY: &[u8; 24] = b"266126f0ebb836dbcff05110";

static ENCRYPTED_BASE64: &'static str = "4OLRDw2hg3CyK7II2I6Y2zEHH7LDqw/gQb8kOXEAqT9ULt0Ks66atiVnMgx5yWntPVq8hYREfDMXl0RRac5t8i7ro6zZY46OGUHyC2OvBPnbZPwAfX3hCKiT8BbEhp88XUBB/k2AEiefw9c25MaTp1S121vNub2N5tdOj6dd4SEpz7iB8Hm6V2MdUECVUZ/6a8HMRCLOtD9JSXFSce8/bucO3Ip+rFUP6bKaDzZ5peIRe+MiuHUqt6w1lXS0S8wRov9N8QkQq9/AIcY6qhwpFO7puqYCt7x3mRL1Q9sfS5su3q/NiBLmB8u+4UwnngfBiupjwmkq072iZItefHMpjBRMlzkCw1N0/32XnIi0jFKGVE9SBOMReFxtX0xsh5iRfg/xxtOJui6kV/xe015tjAMonYklWL9xwaueBXJZhcf9xZssmJzx5MR25p6eIoeiO1TQhy3oJiH3/OC3xD7+1ZZJepN8hKx+bTwdZzUxZ/cRjlShrEF0pojauFgunrNNmjdUbaNXa4Uk/LhdHrxci4RH8BKjiuJ0pWULdHh6xDV8cMMS30INDFT0JG4OqZCRFKBtOlSw8VxqQd/mBRSBlZZ6VdsVS2tpyGHurimGvac=";

fn get_text_byte_stream(
) -> impl futures::Stream<Item = Result<bytes::Bytes, Box<dyn std::error::Error>>> + Send {
    futures::stream::iter(
        TEST_STRING
        .as_bytes()
        .chunks(16)
        .map(bytes::Bytes::copy_from_slice)
        .map(|x| Ok(x)),
    )
}


#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let crypto = AsyncEncryption::new(
        openssl::symm::Cipher::des_ede3_cbc(),
        TEST_KEY,
        Some(b"bcff0511"),
    );

    let mut bytes_stream = get_text_byte_stream();

    let mut enc_stream = crypto.encrypt_stream(&mut bytes_stream)?;

    let mut enc_bytes = Vec::new();

    while let Some(Ok(part)) = enc_stream.next().await {
        enc_bytes.extend_from_slice(part.deref());
    }

    assert_eq!(ENCRYPTED_BASE64, &openssl::base64::encode_block(&enc_bytes));
    Ok(())
}

Contributing

🎈 Contributions are very welcome to improve the project.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, shall be licensed as MIT, without any additional terms or conditions.

I'm looking to get hired

If you like my work, please let me know by recommending me for rust jobs.

Dependencies

~2.7–4MB
~83K SLoC