secret/hex
constant-time hex codecs whose output is itself a Secret.
Since: 0.1.0
hex-encode-raw!
(hex-encode-raw! [p ptr<void> n : int out ptr<void>] :)
write 2*n lowercase hex chars for n bytes at `p`.
| p | source bytes | |
| n | source length | |
| out | destination, at least 2*n bytes (no NUL is written) |
The number of characters written (2*n). It returns a count rather than `:void` so it can be sequenced in a `let` binding list -- binding a :void call emits `void x = ...` and fails the C compile.
(hex-encode-raw! key 32 buf) ; => 64
Since: 0.1.0
hex-decode-raw!
(hex-decode-raw! [p ptr<void> n : int out ptr<void>] :)
decode 2*n hex chars at `p` into n bytes at `out`.
| p | source hex characters | |
| n | number of BYTES to produce (source must hold 2*n characters) | |
| out | destination, at least n bytes |
true when every character was a valid hex digit. On false the destination is wiped rather than left holding a partial decode.
(hex-decode-raw! "ff00" 2 buf) ; => true, buf = {0xff, 0x00}
Since: 0.1.0
secret->hex
(secret->hex [^borrow s : Secret] :)
lowercase hex of a borrowed secret, as a new Secret.
| s | the secret, borrowed (not consumed) |
(Result Secret cstr) -- ok carries a 2n-byte Secret of hex characters (not NUL-terminated).
(let [r (secret->hex k)]
(if (ok? r) (ok-val r) ...))
Since: 0.1.0
secret-of-hex
(secret-of-hex [c : cstr] :)
decode a hex string into a new Secret.
| c | hex characters, upper or lower case, even length |
(Result Secret cstr) -- ok carries the decoded bytes; err on an odd length, an empty string, or any non-hex character.
(secret-of-hex "deadbeef") ; => ok, a 4-byte Secret
Since: 0.1.0
Internal definitions
hex-wipe-raw!-- local wipe shim so hex-decode-raw! can clear a failedcstr-len-- byte length of a NUL-terminated string.cstr-ptr-- view a cstr as a raw byte pointer.encode-then-wrap-- encode into scratch, copy into a Secret, free scratch.decode-then-wrap-- decode into scratch, copy into a Secret, free scratch.wrap-decoded-- copy n decoded bytes into a Secret, then free the scratch.fail-decoded-- release the scratch and report a malformed input.