feat(crypto): sha256/sha512 return raw bytes; add ard/hex module#83
Merged
feat(crypto): sha256/sha512 return raw bytes; add ard/hex module#83
Conversation
BREAKING CHANGE: crypto::sha256 and crypto::sha512 now return the raw digest bytes instead of a hex string. Raw bytes are the semantically correct output and match Node/Python defaults. To migrate existing hex output, wrap calls with the new ard/hex module: // Before let digest = crypto::sha256(input) // hex string // After let digest = hex::encode(crypto::sha256(input)) // same hex string md5 is unchanged for now (still returns hex). The new ard/hex module provides: hex::encode(bytes: Str) Str // raw bytes -> lowercase hex hex::decode(input: Str) Str!Str // hex -> raw bytes (Result) Together these make PKCE S256 a one-liner: base64::encode_url(crypto::sha256(verifier), true) Verified end-to-end against RFC 7636 Appendix B.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two paired changes:
crypto::sha256andcrypto::sha512now return raw digest bytes instead of hex strings. This is a breaking change but aligns Ard with Node/Python defaults where digests are bytes, and lets the hash output feed directly into base64/hex without a decoding dance.New
ard/hexstdlib module withencode/decode, added in the same release so the previous hex behavior is still one function call away.Motivation
PKCE S256 (OAuth 2.1) needs
base64url(sha256(verifier))with no padding. Before this change, you had to hex-decode the sha256 result to get bytes, then re-encode as base64url:After:
Verified end-to-end against RFC 7636 Appendix B — produces the expected challenge
E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM.This unblocks MCP server OAuth work in Ranger.
Breaking Change
Migration
Wrap existing calls in the new
hex::encodeto get the same hex output as before:What's unchanged
crypto::md5still returns a hex string. The TODO only listed sha256/sha512, and md5 has different (and rarer) usage patterns. Happy to change md5 for consistency in a follow-up if you want.crypto::hash/verify(bcrypt) — unchanged (they return opaque strings, not bytes).crypto::scrypt_*— unchanged (they still return<salt>:<hex>format).New module:
ard/hexSymmetric with
ard/base64. Decode returnsStr!Strso bad input is a recoverable error, not a panic.Tests
std_lib/hex.ard: 7 inline tests (roundtrip, lowercase output, empty, odd-length rejection, non-hex character rejection)std_lib/crypto.ard: sha256/sha512 tests updated to wrap withhex::encodefor the hex assertion, plus 2 new tests verifying raw-byte length (32 and 64)All 66 stdlib tests pass (was 55). All Go tests pass.
Docs
website/src/content/docs/stdlib/hex.mdwebsite/src/content/docs/stdlib/crypto.md— sha256/sha512 sections describe new behavior, added PKCE exampleard/hexentryRelease note
Needs a minor bump (pre-1.0, breaking change). The migration is a single wrap with
hex::encode, so the blast radius is small.