///|
/// Return the canonical UTF-8 bytes of a parsed value.
pub fn canonical_bytes(value : JsonValue) -> Result[Bytes, CanonicalError] {
let canonical = match canonicalize(value) {
Ok(canonical) => canonical
Err(error) => return Err(error)
}
Ok(@utf8.encode(canonical))
}
///|
pub fn canonical_byte_length(value : JsonValue) -> Result[Int, CanonicalError] {
match canonical_bytes(value) {
Ok(bytes) => Ok(bytes.length())
Err(error) => Err(error)
}
}
///|
/// Hash arbitrary UTF-8 bytes with the same lowercase SHA-256 representation
/// used by `canonical_digest`.
pub fn sha256_bytes(bytes : Bytes) -> String {
let digest = @sha256.Digest::new()
for byte in bytes {
digest.write(byte)
}
digest.check_sum()
}
///|
pub fn canonical_digest_bytes(
value : JsonValue,
) -> Result[String, CanonicalError] {
match canonical_bytes(value) {
Ok(bytes) => Ok(sha256_bytes(bytes))
Err(error) => Err(error)
}
}