DoxigAlpha

encode

Encodes a binary buffer into a hexadecimal string. The output buffer must be twice the size of the input buffer.

Function parameters

Parameters

#
encoded:[]u8
bin:[]const u8
case:std.fmt.Case

(best-effort) constant time hexadecimal encoding and decoding.

Types

#
hex
(best-effort) constant time hexadecimal encoding and decoding.
base64
(best-effort) constant time base64 encoding and decoding.

Error sets in this namespace

Error Sets

#

Source

Implementation

#
pub fn encode(encoded: []u8, bin: []const u8, comptime case: std.fmt.Case) error{SizeMismatch}!void {
    if (encoded.len / 2 != bin.len) {
        return error.SizeMismatch;
    }
    for (bin, 0..) |v, i| {
        const b: u16 = v >> 4;
        const c: u16 = v & 0xf;
        const off = if (case == .upper) 32 else 0;
        const x =
            ((87 - off + c + (((c -% 10) >> 8) & ~@as(u16, 38 - off))) & 0xff) << 8 |
            ((87 - off + b + (((b -% 10) >> 8) & ~@as(u16, 38 - off))) & 0xff);
        encoded[i * 2] = @truncate(x);
        encoded[i * 2 + 1] = @truncate(x >> 8);
    }
}