fire_crypto/
lib.rs

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