This repository has no description
0

Configure Feed

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

webauthn / src / challenge.zig
1.7 kB 51 lines
1const std = @import("std"); 2const base64url = @import("base64url.zig"); 3 4pub const default_size = 32; 5 6pub fn generateSecure(allocator: std.mem.Allocator, io: std.Io) ![]u8 { 7 return generateSecureWithSize(allocator, io, default_size); 8} 9 10pub fn generateSecureWithSize(allocator: std.mem.Allocator, io: std.Io, size: usize) ![]u8 { 11 const bytes = try allocator.alloc(u8, size); 12 defer allocator.free(bytes); 13 try io.randomSecure(bytes); 14 return base64url.encodeAlloc(allocator, bytes); 15} 16 17pub fn generate(allocator: std.mem.Allocator, random: std.Random) ![]u8 { 18 return generateWithSize(allocator, random, default_size); 19} 20 21pub fn generateWithSize(allocator: std.mem.Allocator, random: std.Random, size: usize) ![]u8 { 22 const bytes = try allocator.alloc(u8, size); 23 defer allocator.free(bytes); 24 random.bytes(bytes); 25 return base64url.encodeAlloc(allocator, bytes); 26} 27 28test "generates url-safe challenge" { 29 const allocator = std.testing.allocator; 30 var prng = std.Random.DefaultPrng.init(0x12345678); 31 const value = try generate(allocator, prng.random()); 32 defer allocator.free(value); 33 34 try std.testing.expect(value.len >= 42); 35 for (value) |byte| { 36 try std.testing.expect(std.ascii.isAlphanumeric(byte) or byte == '-' or byte == '_'); 37 } 38} 39 40test "generates secure challenge from Io" { 41 var threaded = std.Io.Threaded.init(std.testing.allocator, .{}); 42 defer threaded.deinit(); 43 44 const value = try generateSecure(std.testing.allocator, threaded.io()); 45 defer std.testing.allocator.free(value); 46 47 try std.testing.expect(value.len >= 42); 48 for (value) |byte| { 49 try std.testing.expect(std.ascii.isAlphanumeric(byte) or byte == '-' or byte == '_'); 50 } 51}