use subtle::ConstantTimeEq;
pub fn generate(length: usize) -> (String, String) {
let mut code = String::with_capacity(length);
for _ in 0..length {
let mut byte = [0u8; 1];
loop {
rand::fill(&mut byte);
if byte[0] < 250 {
code.push((b'0' + (byte[0] % 10)) as char);
break;
}
}
}
let hash = sha256_hex(&code);
(code, hash)
}
pub fn verify(code: &str, hash: &str) -> bool {
let computed = sha256_hex(code);
computed.as_bytes().ct_eq(hash.as_bytes()).into()
}
fn sha256_hex(input: &str) -> String {
crate::encoding::hex::sha256(input)
}