#hybrid #tink #public-key

tink-hybrid

Hybrid encryption functionality for Rust port of Google's Tink cryptography library

3 releases

0.2.5 Mar 14, 2023
0.2.4 Mar 25, 2022
0.2.3 Jan 3, 2022

#1925 in Cryptography

Download history 2/week @ 2023-12-18 7/week @ 2023-12-25 17/week @ 2024-01-08 43/week @ 2024-01-15 21/week @ 2024-01-22 16/week @ 2024-01-29 56/week @ 2024-02-05 100/week @ 2024-02-12 101/week @ 2024-02-19 163/week @ 2024-02-26 77/week @ 2024-03-04 33/week @ 2024-03-11 23/week @ 2024-03-18 85/week @ 2024-03-25 62/week @ 2024-04-01

206 downloads per month
Used in rinkey

Apache-2.0

310KB
5K SLoC

Tink-Rust: Hybrid Encryption

Docs MSRV

This crate provides hybrid encryption functionality, as described in the upstream Tink documentation.

Usage

fn main() -> Result<(), Box<dyn Error>> {
    tink_hybrid::init();
    let kh_priv = tink_core::keyset::Handle::new(
        &tink_hybrid::ecies_hkdf_aes128_ctr_hmac_sha256_key_template(),
    )?;

    // NOTE: save the private keyset to a safe location. DO NOT hardcode it in source code.
    // Consider encrypting it with a remote key in Cloud KMS, AWS KMS or HashiCorp Vault.  See
    // https://github.com/google/tink/blob/master/docs/GOLANG-HOWTO.md#storing-and-loading-existing-keysets.

    let kh_pub = kh_priv.public()?;

    // NOTE: share the public keyset with the sender.

    let enc = tink_hybrid::new_encrypt(&kh_pub)?;

    let msg = b"this data needs to be encrypted";
    let encryption_context = b"encryption context";
    let ct = enc.encrypt(msg, encryption_context)?;

    let dec = tink_hybrid::new_decrypt(&kh_priv)?;

    let pt = dec.decrypt(&ct, encryption_context)?;
    assert_eq!(msg[..], pt);

    println!("Ciphertext: {}\n", hex::encode(&ct));
    println!("Original  plaintext: {}\n", String::from_utf8_lossy(msg));
    println!("Decrypted plaintext: {}\n", String::from_utf8_lossy(&pt));
    Ok(())
}

License

Apache License, Version 2.0

Disclaimer

This is not an officially supported Google product.

Dependencies

~4.5–7MB
~125K SLoC