Skip to main content

vta_sdk/
hex.rs

1//! Minimal hex helpers used across the SDK and CLI binaries.
2//!
3//! The workspace used to carry six copies of `hex_lower` (vta-sdk,
4//! vta-cli-common × 2, pnm-cli, vta-service × 2). Centralising here
5//! ensures the canonical-lowercase digest format used everywhere
6//! (bundle ids, SHA-256 digests, nonces) doesn't drift between call
7//! sites.
8
9/// Encode a byte slice as lowercase hex, two chars per byte.
10///
11/// Wrapped for portability rather than depending on a third-party `hex`
12/// crate — the function is 10 lines and the workspace already has its
13/// own multibase stack for anything more structured.
14pub fn lower(bytes: &[u8]) -> String {
15    const T: &[u8; 16] = b"0123456789abcdef";
16    let mut s = String::with_capacity(bytes.len() * 2);
17    for &b in bytes {
18        s.push(T[(b >> 4) as usize] as char);
19        s.push(T[(b & 0xf) as usize] as char);
20    }
21    s
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn lower_formats_bytes() {
30        assert_eq!(lower(&[0x0a, 0xff, 0x00]), "0aff00");
31        assert_eq!(lower(&[]), "");
32        assert_eq!(lower(&[0x00]), "00");
33    }
34}