authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-03-29 09:33:22+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-03-29 09:33:22+02:00
logdcb33abc2ce1589855ed7a70c31d5bffc0dec422
tree58c1c970097ab50e3eae3bbfeb65d3fccefca6a7
parent4b85f05f060ee3365bd86e1885bbefb9e685c81b

crypto.base64: use "-" instead of "+" character in URL-safe mode

/ was turned into _, but + also needs to be turned into -

1 files changed, 12 insertions(+), 2 deletions(-)

lib/std/crypto/codecs/base64_hex_ct.zig+12-2
......@@ -304,7 +304,7 @@ pub const base64 = struct {
304304 return (lt(x, 26) & (x +% 'A')) |
305305 (ge(x, 26) & lt(x, 52) & (x +% 'a' -% 26)) |
306306 (ge(x, 52) & lt(x, 62) & (x +% '0' -% 52)) |
307 (eq(x, 62) & '+') | (eq(x, 63) & if (urlsafe) '_' else '/');
307 (eq(x, 62) & if (urlsafe) '-' else '+') | (eq(x, 63) & if (urlsafe) '_' else '/');
308308 }
309309
310310 fn byteFromChar(c: u8, comptime urlsafe: bool) u8 {
......@@ -312,7 +312,7 @@ pub const base64 = struct {
312312 (ge(c, 'A') & le(c, 'Z') & (c -% 'A')) |
313313 (ge(c, 'a') & le(c, 'z') & (c -% 'a' +% 26)) |
314314 (ge(c, '0') & le(c, '9') & (c -% '0' +% 52)) |
315 (eq(c, '+') & 62) | (eq(c, if (urlsafe) '_' else '/') & 63);
315 (eq(c, if (urlsafe) '-' else '+') & 62) | (eq(c, if (urlsafe) '_' else '/') & 63);
316316 return x | (eq(x, 0) & ~eq(c, 'A'));
317317 }
318318
......@@ -453,6 +453,16 @@ test "hex with ignored chars" {
453453 try testing.expectEqualSlices(u8, &expected, bin);
454454}
455455
456test "base64 urlsafe" {
457 const input = [_]u8{ 0xfb, 0xff };
458 var enc_buf: [4]u8 = undefined;
459 var dec_buf: [2]u8 = undefined;
460 const encoded = try base64.encode(&enc_buf, &input, .urlsafe);
461 try testing.expectEqualSlices(u8, "-_8=", encoded);
462 const decoded = try base64.decode(&dec_buf, encoded, .urlsafe);
463 try testing.expectEqualSlices(u8, &input, decoded);
464}
465
456466test "base64 with ignored chars" {
457467 const encoded = "dGVzdCBi\r\nYXNlNjQ=\n";
458468 const expected = "test base64";