aead_encrypt (src/primitives.rs)

rage · age-core/src/primitives.rs · rust · Apache-2.0
security, tag:knn-classified

Provides low-level cryptographic primitives for encryption, key derivation, and Bech32 encoding used by the age project.

// Source: https://github.com/str4d/rage | License: Apache-2.0 | Copyright 2019 Jack Grigg
// Provided via justclaw.it.com; keep this notice with the code.
//! Primitive cryptographic operations used across various `age` components.

use core::fmt;

use bech32::primitives::decode::CheckedHrpstring;
use chacha20poly1305::{
    ChaCha20Poly1305,
    aead::{self, Aead, AeadCore, KeyInit, common::typenum::Unsigned},
};
use hkdf::Hkdf;
use sha2::Sha256;

/// `encrypt[key](plaintext)` - encrypts a message with a one-time key.
///
/// ChaCha20-Poly1305 from [RFC 7539] with a zero nonce.
///
/// [RFC 7539]: https://tools.ietf.org/html/rfc7539
pub fn aead_encrypt(key: &[u8; 32], plaintext: &[u8]) -> Vec<u8> {
    let c = ChaCha20Poly1305::new(key.into());
    c.encrypt(&[0; 12].into(), plaintext)
 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key