doc: module doc + readme

This commit is contained in:
Jixun Wu
2021-12-28 23:07:20 +00:00
parent 00c5b318fd
commit ab6bca0ba6
5 changed files with 23 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
//! _Tencent modified TEA_ (tc_tea) is a variant of the standard TEA (Tiny Encryption Algorithm).
//!
//! Notably, it uses a different round number and uses a "tweaked" CBC mode.
mod stream_ext;

View File

@@ -8,6 +8,9 @@ const SALT_LEN: usize = 2;
const ZERO_LEN: usize = 7;
const FIXED_PADDING_LEN: usize = 1 + SALT_LEN + ZERO_LEN;
/// Calculate expected size of encrypted data.
///
/// `body_size` is the size of data you'd like to encrypt.
pub fn calc_encrypted_size(body_size: usize) -> usize {
let len = FIXED_PADDING_LEN + body_size;
let pad_len = (8 - (len & 0b0111)) & 0b0111;
@@ -15,13 +18,16 @@ pub fn calc_encrypted_size(body_size: usize) -> usize {
}
/// Encrypts an arbitrary length sized data in the following way:
///
/// * PadLen (1 byte)
/// * Padding (variable, 0-7byte)
/// * Salt (2 bytes)
/// * Body (? bytes)
/// * Zero (7 bytes)
/// PadLen/Padding/Salt is random bytes. Minimum of 3 bytes.
/// PadLen is taken from the last 3 bit of the first byte.
///
/// Returned bytes will always have a length multiple of 8.
///
/// PadLen/Padding/Salt are randomly bytes, with a minimum of 21 bits (3 * 8 - 3) randomness.
///
/// # Panics
///
@@ -78,12 +84,13 @@ pub fn encrypt<T: AsRef<[u8]>, K: AsRef<[u8]>>(plaintext: T, key: K) -> Option<B
}
/// Decrypts a byte array containing the following:
///
/// * PadLen (1 byte)
/// * Padding (variable, 0-7byte)
/// * Salt (2 bytes)
/// * Body (? bytes)
/// * Zero (7 bytes)
/// PadLen/Padding/Salt is random bytes. Minimum of 3 bytes.
///
/// PadLen is taken from the last 3 bit of the first byte.
pub fn decrypt<T: AsRef<[u8]>, K: AsRef<[u8]>>(encrypted: T, key: K) -> Option<Box<[u8]>> {
let encrypted = encrypted.as_ref();