use super::funejson::Value; use super::encoding_base32_base32_encode::base32_encode; /// A new secret from the caller's random bytes, as unpadded base32. A pure /// function cannot read randomness itself. /// /// # Panics /// Panics on a value outside 0-255, or fewer than 16 bytes. pub fn totp_secret(random_bytes: &[i64]) -> String { if random_bytes.iter().any(|b| !(0..=255).contains(b)) { panic!("randomBytes must be a list of integers from 0 to 255"); } if random_bytes.len() < 16 { panic!("randomBytes must hold at least 16 bytes (128 bits)"); } base32_encode(random_bytes, false) } pub fn fune_vector(args: &[Value]) -> Value { let bytes: Vec = match &args[0] { Value::Arr(items) => items .iter() .map(|item| match item { Value::Int(i) => *i, _ => panic!("randomBytes must be a list of integers from 0 to 255"), }) .collect(), _ => panic!("randomBytes must be a list of integers from 0 to 255"), }; Value::str(&totp_secret(&bytes)) }