Functional Weave
Code in Python

auth.totp@1.0.0

impl/rust/totp_secret.rs

1,092 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
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<i64> = 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))
}