forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.7 MiB37e985dTahinli2 hours ago363 commits

signed_token.rs25 lines, 702 bytesHistoryRawDownload
1use hmac::{Hmac, KeyInit, Mac};
2use sha2::Sha256;
3use subtle::ConstantTimeEq;
4
5type HmacSha256 = Hmac<Sha256>;
6
7pub const MAC_LEN: usize = 32;
8
9pub fn mac(key: &[u8; 32], parts: &[&[u8]]) -> Vec<u8> {
10 let mut mac =
11 <HmacSha256 as KeyInit>::new_from_slice(key).expect("HMAC accepts any key length");
12 for part in parts {
13 mac.update(part);
14 }
15 mac.finalize().into_bytes().to_vec()
16}
17
18pub fn verify(key: &[u8; 32], parts: &[&[u8]], tag: &[u8]) -> bool {
19 tag.len() == MAC_LEN && bool::from(mac(key, parts).as_slice().ct_eq(tag))
20}
21
22pub fn parse_expiry(text: &str) -> Option<i64> {
23 let value: i64 = text.parse().ok()?;
24 (value.to_string() == text).then_some(value)
25}