Skip to main content

chuchi_crypto/
lib.rs

1#![doc = include_str!("../README.md")]
2#![allow(clippy::new_without_default)]
3
4use rand::Rng;
5
6/// used internally when b64
7#[cfg(feature = "b64")]
8use std::str::FromStr;
9
10use crate::utils::SysRngPanic;
11
12#[cfg(feature = "cipher")]
13pub mod cipher;
14
15#[cfg(feature = "signature")]
16pub mod signature;
17
18#[cfg(feature = "hash")]
19pub mod hash;
20
21pub mod token;
22
23pub mod error;
24
25mod utils;
26
27// from https://docs.rs/crate/chacha20/0.3.4/source/src/cipher.rs
28/// Xors two buffers. Both buffers need to have the same length.
29///
30/// ## Panics
31/// When the buffers don't have the same length.
32pub fn xor(buf: &mut [u8], key: &[u8]) {
33	assert_eq!(buf.len(), key.len());
34
35	for (a, b) in buf.iter_mut().zip(key) {
36		*a ^= *b;
37	}
38}
39
40/// Fills a slice with random bytes.
41///
42/// ## OsRng
43/// This a cryptographically secure random number generator.
44/// Which uses the operating system's random number generator.
45/// [See](https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html)
46pub fn fill_random(buf: &mut [u8]) {
47	SysRngPanic.fill_bytes(buf)
48}
49
50/// Since this function multiplies s with 4
51/// s needs to be 1/4 of usize::MAX in practice this should not be a problem
52/// since the tokens won't be that long.
53#[inline(always)]
54const fn calculate_b64_len(s: usize) -> usize {
55	(4 * s).div_ceil(3)
56}