This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

webauthn / src / base64url.zig
1.1 kB 31 lines
1const std = @import("std"); 2 3pub fn encodeAlloc(allocator: std.mem.Allocator, bytes: []const u8) ![]u8 { 4 const len = std.base64.url_safe_no_pad.Encoder.calcSize(bytes.len); 5 const out = try allocator.alloc(u8, len); 6 _ = std.base64.url_safe_no_pad.Encoder.encode(out, bytes); 7 return out; 8} 9 10pub fn decodeAlloc(allocator: std.mem.Allocator, text: []const u8) ![]u8 { 11 const len = std.base64.url_safe_no_pad.Decoder.calcSizeForSlice(text) catch { 12 return error.InvalidBase64Url; 13 }; 14 const out = try allocator.alloc(u8, len); 15 errdefer allocator.free(out); 16 std.base64.url_safe_no_pad.Decoder.decode(out, text) catch { 17 return error.InvalidBase64Url; 18 }; 19 return out; 20} 21 22test "round trips unpadded base64url" { 23 const allocator = std.testing.allocator; 24 const encoded = try encodeAlloc(allocator, "hello?"); 25 defer allocator.free(encoded); 26 try std.testing.expectEqualStrings("aGVsbG8_", encoded); 27 28 const decoded = try decodeAlloc(allocator, encoded); 29 defer allocator.free(decoded); 30 try std.testing.expectEqualStrings("hello?", decoded); 31}