use subtle::ConstantTimeEq;
pub(crate) fn verify_sha256_hex(plain: impl AsRef<[u8]>, stored_hex: &str) -> bool {
let computed = crate::encoding::hex::sha256(plain.as_ref());
computed.as_bytes().ct_eq(stored_hex.as_bytes()).into()
}
pub(crate) fn random_string(alphabet: &[u8], len: usize) -> String {
assert!(!alphabet.is_empty(), "alphabet must not be empty");
let n = alphabet.len();
let bias_limit: usize = (256 / n) * n;
let mut out = String::with_capacity(len);
let mut buf = [0u8; 1];
while out.len() < len {
rand::fill(&mut buf[..]);
let b = buf[0] as usize;
if b < bias_limit {
out.push(alphabet[b % n] as char);
}
}
out
}