diff options
-rw-r--r-- | src/metainfo.zig | 10 |
1 files 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); } |