diff --git a/src/lib.zig b/src/lib.zig index 9652d8214220a454ca07618f792457a6fb64ad01..7a441fa4457f706f8866ccd6337e8a4ac253b00b 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -139,48 +139,17 @@ pub fn fileSize(dir: std.fs.Dir, sub_path: string) !u64 { return s.size; } -pub const HashFn = enum { - blake3, - gimli, - md5, - sha1, - sha224, - sha256, - sha384, - sha512, - sha3_224, - sha3_256, - sha3_384, - sha3_512, -}; - -pub fn hashFile(alloc: std.mem.Allocator, dir: std.fs.Dir, sub_path: string, comptime algo: HashFn) !string { +pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 { const file = try dir.openFile(sub_path, .{}); defer file.close(); - const hash = std.crypto.hash; - const Algo = switch (algo) { - .blake3 => hash.Blake3, - .gimli => hash.Gimli, - .md5 => hash.Md5, - .sha1 => hash.Sha1, - .sha224 => hash.sha2.Sha224, - .sha256 => hash.sha2.Sha256, - .sha384 => hash.sha2.Sha384, - .sha512 => hash.sha2.Sha512, - .sha3_224 => hash.sha3.Sha3_224, - .sha3_256 => hash.sha3.Sha3_256, - .sha3_384 => hash.sha3.Sha3_384, - .sha3_512 => hash.sha3.Sha3_512, - }; var h = Algo.init(.{}); var out: [Algo.digest_length]u8 = undefined; try pipe(file.reader(), h.writer()); h.final(&out); - var res: [Algo.digest_length * 2]u8 = undefined; var fbs = std.io.fixedBufferStream(&res); try std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&out)}); - return try alloc.dupe(u8, &res); + return res; } pub fn pipe(reader_from: anytype, writer_to: anytype) !void {