| ... | ... | @@ -170,10 +170,9 @@ pub fn hashFile(alloc: std.mem.Allocator, dir: std.fs.Dir, sub_path: string, com |
| 170 | 170 | .sha3_384 => hash.sha3.Sha3_384, |
| 171 | 171 | .sha3_512 => hash.sha3.Sha3_512, |
| 172 | 172 | }; |
| 173 | | const h = &Algo.init(.{}); |
| 173 | var h = Algo.init(.{}); |
| 174 | 174 | var out: [Algo.digest_length]u8 = undefined; |
| 175 | | var hw = HashWriter(Algo){ .h = h }; |
| 176 | | try pipe(file.reader(), hw.writer()); |
| 175 | try pipe(file.reader(), h.writer()); |
| 177 | 176 | h.final(&out); |
| 178 | 177 | |
| 179 | 178 | var res: [Algo.digest_length * 2]u8 = undefined; |
| ... | ... | @@ -182,25 +181,6 @@ pub fn hashFile(alloc: std.mem.Allocator, dir: std.fs.Dir, sub_path: string, com |
| 182 | 181 | return try alloc.dupe(u8, &res); |
| 183 | 182 | } |
| 184 | 183 | |
| 185 | | fn HashWriter(comptime T: type) type { |
| 186 | | return struct { |
| 187 | | h: *T, |
| 188 | | |
| 189 | | const Self = @This(); |
| 190 | | pub const Error = error{}; |
| 191 | | pub const Writer = std.io.Writer(*Self, Error, write); |
| 192 | | |
| 193 | | fn write(self: *Self, bytes: []const u8) Error!usize { |
| 194 | | self.h.update(bytes); |
| 195 | | return bytes.len; |
| 196 | | } |
| 197 | | |
| 198 | | pub fn writer(self: *Self) Writer { |
| 199 | | return .{ .context = self }; |
| 200 | | } |
| 201 | | }; |
| 202 | | } |
| 203 | | |
| 204 | 184 | pub fn pipe(reader_from: anytype, writer_to: anytype) !void { |
| 205 | 185 | var buf: [std.mem.page_size]u8 = undefined; |
| 206 | 186 | var fifo = std.fifo.LinearFifo(u8, .Slice).init(&buf); |