Skip to content

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/crypto
use ard/hex
use ard/io
fn main() {
let digest = crypto::sha256("hello".bytes())
io::print(hex::encode(digest))
}

Encode bytes to lowercase hexadecimal text.

hex::encode("".bytes()) // ""
hex::encode("abc".bytes()) // "616263"
hex::encode("hello".bytes()) // "68656c6c6f"

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() // true
hex::decode("zz").is_err() // true
use ard/base64
use ard/crypto
use 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)
}