Skip to content

Commit 8f4ed64

Browse files
committed
fixed
1 parent 64058b9 commit 8f4ed64

File tree

7 files changed

+17
-35
lines changed

7 files changed

+17
-35
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = .zig_jwt,
33
.description = "A JWT (JSON Web Token) library for zig.",
4-
.version = "1.0.20",
4+
.version = "1.0.21",
55
.fingerprint = 0x30a5aec248bd7ac3,
66
.minimum_zig_version = "0.14.0-dev.3451+d8d2aa9af",
77
.dependencies = .{},

src/blake2b.zig

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,15 @@ pub fn SignBlake2b(comptime Hash: type, comptime name: []const u8) type {
3535
return error.JWTKeyTooShort;
3636
}
3737

38+
var out: [digest_length]u8 = undefined;
39+
3840
var h = Hash.init(.{
3941
.key = key,
4042
});
4143
h.update(msg[0..]);
42-
43-
var out: [digest_length]u8 = undefined;
4444
h.final(out[0..]);
4545

46-
const out_string = try self.alloc.alloc(u8, @as(usize, @intCast(self.signLength())));
47-
@memcpy(out_string[0..], out[0..]);
48-
49-
return out_string;
46+
return self.alloc.dupe(u8, out[0..]);
5047
}
5148

5249
pub fn verify(self: Self, msg: []const u8, signature: []u8, key: []const u8) bool {
@@ -59,12 +56,12 @@ pub fn SignBlake2b(comptime Hash: type, comptime name: []const u8) type {
5956
return false;
6057
}
6158

59+
var out: [digest_length]u8 = undefined;
60+
6261
var h = Hash.init(.{
6362
.key = key,
6463
});
6564
h.update(msg[0..]);
66-
67-
var out: [digest_length]u8 = undefined;
6865
h.final(out[0..]);
6966

7067
if (std.mem.eql(u8, out[0..], signature[0..])) {

src/ecdsa.zig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@ pub fn SignECDSA(comptime EC: type, comptime name: []const u8) type {
3939
var secret_key = try EC.KeyPair.fromSecretKey(key);
4040

4141
const sig = try secret_key.sign(msg[0..], null);
42-
const out: [encoded_length]u8 = sig.toBytes();
42+
const out = sig.toBytes();
4343

44-
const out_string = try self.alloc.alloc(u8, @as(usize, @intCast(self.signLength())));
45-
@memcpy(out_string[0..], out[0..]);
46-
47-
return out_string;
44+
return self.alloc.dupe(u8, out[0..]);
4845
}
4946

5047
pub fn verify(self: Self, msg: []const u8, signature: []u8, key: EC.PublicKey) bool {

src/eddsa.zig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ pub fn SignEdDSA(comptime name: []const u8) type {
3636
var secret_key = try Ed25519.KeyPair.fromSecretKey(key);
3737

3838
const sig = try secret_key.sign(msg[0..], null);
39-
var out: [encoded_length]u8 = sig.toBytes();
39+
var out = sig.toBytes();
4040

41-
const out_string = try self.alloc.alloc(u8, @as(usize, @intCast(self.signLength())));
42-
@memcpy(out_string[0..], out[0..]);
43-
44-
return out_string;
41+
return self.alloc.dupe(u8, out[0..]);
4542
}
4643

4744
pub fn verify(self: Self, msg: []const u8, signature: []u8, key: Ed25519.PublicKey) bool {

src/hmac.zig

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ pub fn SignHmac(comptime Hash: type, comptime name: []const u8) type {
3636
}
3737

3838
pub fn sign(self: Self, msg: []const u8, key: []const u8) ![]u8 {
39+
var out: [mac_length]u8 = undefined;
40+
3941
var h = Hash.init(key);
4042
h.update(msg[0..]);
41-
42-
var out: [mac_length]u8 = undefined;
4343
h.final(out[0..]);
4444

45-
const out_string = try self.alloc.alloc(u8, @as(usize, @intCast(self.signLength())));
46-
@memcpy(out_string[0..], out[0..]);
47-
48-
return out_string;
45+
return self.alloc.dupe(u8, out[0..]);
4946
}
5047

5148
pub fn verify(self: Self, msg: []const u8, signature: []u8, key: []const u8) bool {
@@ -54,10 +51,10 @@ pub fn SignHmac(comptime Hash: type, comptime name: []const u8) type {
5451
return false;
5552
}
5653

54+
var out: [mac_length]u8 = undefined;
55+
5756
var h = Hash.init(key);
5857
h.update(msg[0..]);
59-
60-
var out: [mac_length]u8 = undefined;
6158
h.final(out[0..]);
6259

6360
if (std.mem.eql(u8, out[0..], signature[0..])) {

src/rsa.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ pub fn SignRSA(comptime RSAType: type, comptime name: []const u8) type {
4949

5050
const signed = sig.toBytes();
5151

52-
const out_string = try self.alloc.alloc(u8, signed.len);
53-
@memcpy(out_string[0..], signed[0..]);
54-
55-
return out_string;
52+
return self.alloc.dupe(u8, signed[0..]);
5653
}
5754

5855
pub fn verify(self: Self, msg: []const u8, signature: []u8, key: rsa.PublicKey) bool {

src/rsa_pss.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ pub fn SignRSAPss(comptime RSAPssType: type, comptime name: []const u8) type {
4848

4949
const signed = sig.toBytes();
5050

51-
const out_string = try self.alloc.alloc(u8, signed.len);
52-
@memcpy(out_string[0..], signed[0..]);
53-
54-
return out_string;
51+
return self.alloc.dupe(u8, signed[0..]);
5552
}
5653

5754
pub fn verify(self: Self, msg: []const u8, signature: []u8, key: rsa.PublicKey) bool {

0 commit comments

Comments
 (0)