Hex encoding with ard/hex
The ard/hex module provides hexadecimal encoding and decoding for [Byte] buffers. It is the standard way to render binary data, such as cryptographic digests, as human-readable text.
use ard/cryptouse ard/hexuse ard/io
fn main() { let digest = crypto::sha256("hello".bytes()) io::print(hex::encode(digest))}fn encode(bytes: [Byte]) Str
Section titled “fn encode(bytes: [Byte]) Str”Encode bytes to lowercase hexadecimal text.
hex::encode("".bytes()) // ""hex::encode("abc".bytes()) // "616263"hex::encode("hello".bytes()) // "68656c6c6f"fn decode(input: Str) [Byte]!Str
Section titled “fn decode(input: Str) [Byte]!Str”Decode hexadecimal text back into bytes. Returns err(message) for odd-length or non-hex input.
let bytes = try hex::decode("68656c6c6f")let text = Str::from_bytes(bytes).expect("utf8") // "hello"
hex::decode("abc").is_err() // truehex::decode("zz").is_err() // trueExamples
Section titled “Examples”use ard/base64use ard/cryptouse ard/hex
fn fingerprint(input: Str) Str { hex::encode(crypto::sha256(input.bytes()))}
fn pkce_challenge(verifier: Str) Str { base64::encode_url(crypto::sha256(verifier.bytes()), true)}