From e51c15cc478b6c89e9100017eebfdf88aff95cca Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sat, 11 Nov 2023 20:54:04 +0000 Subject: Don't hex the hash --- src/metainfo.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/metainfo.zig b/src/metainfo.zig index 31b14b7..29cc655 100644 --- a/src/metainfo.zig +++ b/src/metainfo.zig @@ -103,16 +103,14 @@ pub const Info = struct { a.free(self.files); } - const info_hash_len = 40; + const info_hash_len = std.crypto.hash.Sha1.digest_length; pub fn hash(self: Info, a: std.mem.Allocator) ![info_hash_len]u8 { var b = try self.encode(a); defer b.deinit(a); var sha1 = std.crypto.hash.Sha1.init(.{}); var w = sha1.writer(); try b.bencode(w); - var buf = [_]u8{0} ** info_hash_len; - _ = std.fmt.bufPrint(&buf, "{}", .{std.fmt.fmtSliceHexLower(&sha1.finalResult())}) catch unreachable; - return buf; + return sha1.finalResult(); } }; @@ -162,5 +160,7 @@ test "info hash" { var mi = try MetaInfo.parse(a, b); defer mi.deinit(a); const hash = try mi.info.hash(a); - try std.testing.expectEqualStrings("d69f91e6b2ae4c542468d1073a71d4ea13879a7f", &hash); + var hash_hex = try std.fmt.allocPrint(a, "{s}", .{std.fmt.fmtSliceHexLower(&hash)}); + defer a.free(hash_hex); + try std.testing.expectEqualStrings("d69f91e6b2ae4c542468d1073a71d4ea13879a7f", hash_hex); } -- cgit v1.2.3-ZIG