an http based roguelike rpg about jelqing
Blame: src/secret.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | use std::fmt::Write as _; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 2 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 3 | use hmac::{Hmac, Mac}; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 4 | use sha2::Sha256; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 5 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | pub struct Secret(Vec<u8>); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 7 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 8 | fn hex(bytes: &[u8]) -> String { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 9 | bytes.iter().fold(String::new(), |mut out, b| { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 10 | let _ = write!(out, "{b:02x}"); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 11 | out |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 12 | }) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 13 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 14 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 15 | fn unhex(text: &str) -> Option<Vec<u8>> { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 16 | if !text.len().is_multiple_of(2) { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 17 | return None; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 18 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 19 | (0..text.len()) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 20 | .step_by(2) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 21 | .map(|i| u8::from_str_radix(text.get(i..i + 2)?, 16).ok()) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 22 | .collect() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 23 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 24 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | impl Secret { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | pub fn new(key: Vec<u8>) -> Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | Self(key) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 29 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | pub fn random() -> Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | Self(rand::random::<[u8; 32]>().to_vec()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 33 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | fn mac(&self, message: &str) -> Hmac<Sha256> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | let mut mac = Hmac::<Sha256>::new_from_slice(&self.0).expect("hmac accepts any key length"); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | mac.update(message.as_bytes()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | mac |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | pub fn sign(&self, name: &str, value: &str) -> String { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | let tag = self.mac(&format!("{name}={value}")).finalize().into_bytes(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | format!("{value}.{}", hex(&tag)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | pub fn verify<'a>(&self, name: &str, signed: &'a str) -> Option<&'a str> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | let (value, tag) = signed.rsplit_once('.')?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | self.mac(&format!("{name}={value}")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | .verify_slice(&unhex(tag)?) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | .ok()?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | Some(value) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | /// Mixes the secret into a seed, so a public seed and public source cannot predict rolls offline. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | pub fn mix_seed(&self, seed: u64) -> u64 { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | let tag = self.mac(&format!("seed={seed}")).finalize().into_bytes(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | u64::from_le_bytes(tag[..8].try_into().expect("sha256 is longer than 8 bytes")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 58 | } |