ptth/crates/ptth_core/src/lib.rs

52 lines
1.1 KiB
Rust

//! # PTTH Core
//!
//! Common code used by both `ptth_relay` and `ptth_server`.
#![warn (clippy::pedantic)]
/// Wrapper for graceful and forced shutdowns of `ptth_relay` and `ptth_server`
pub mod graceful_shutdown;
/// An abstraction over HTTP that is easy to (de)serialize
pub mod http_serde;
pub mod prelude;
/// `ptth_server` packs its response headers into this request header
///
/// This allows the server's response body to be wrapped verbatim
/// in the request body.
/// The header value is the response's status code and headers,
/// wrapped in a MessagePack structure, and encoded in base64
/// to make it ASCII.
pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4";
/// Generates 64 bytes of entropy and returns it as Base64
pub fn gen_key () -> String {
use rand::RngCore;
let mut buffer = vec! [0_u8; 64];
rand::thread_rng ().fill_bytes (&mut buffer);
base64::encode (&buffer)
}
#[cfg (test)]
mod tests {
use super::*;
#[test]
fn test_gen_key () {
// Smoke test
let blank = base64::encode (&vec! [0_u8; 64]);
let a = gen_key ();
let b = gen_key ();
assert_ne! (blank, a);
assert_ne! (blank, b);
assert_ne! (a, b);
}
}