| ... | @@ -261,27 +261,20 @@ pub fn validate_hash(input: []const u8, file_path: []const u8) !bool { | ... | @@ -261,27 +261,20 @@ pub fn validate_hash(input: []const u8, file_path: []const u8) !bool { |
| 261 | const file = try std.fs.cwd().openFile(file_path, .{}); | 261 | const file = try std.fs.cwd().openFile(file_path, .{}); |
| 262 | defer file.close(); | 262 | defer file.close(); |
| 263 | const data = try file.reader().readAllAlloc(gpa, gb); | 263 | const data = try file.reader().readAllAlloc(gpa, gb); |
| 264 | return std.mem.eql(u8, hash.string, switch (hash.id) { | 264 | const expected = hash.string; |
| 265 | .blake3 => blk: { | 265 | const actual = switch (hash.id) { |
| 266 | const h = &std.crypto.hash.Blake3.init(.{}); | 266 | .blake3 => try do_hash(std.crypto.hash.Blake3, data), |
| 267 | var out: [32]u8 = undefined; | 267 | .sha256 => try do_hash(std.crypto.hash.sha2.Sha256, data), |
| 268 | h.update(data); | 268 | .sha512 => try do_hash(std.crypto.hash.sha2.Sha512, data), |
| 269 | h.final(&out); | 269 | }; |
| 270 | break :blk try std.fmt.allocPrint(gpa, "{x}", .{out}); | 270 | return std.mem.eql(u8, expected, actual); |
| 271 | }, | 271 | } |
| 272 | .sha256 => blk: { | 272 | |
| 273 | const h = &std.crypto.hash.sha2.Sha256.init(.{}); | 273 | pub fn do_hash(comptime algo: type, data: []const u8) ![]const u8 { |
| 274 | var out: [32]u8 = undefined; | 274 | const h = &algo.init(.{}); |
| 275 | h.update(data); | 275 | var out: [algo.digest_length]u8 = undefined; |
| 276 | h.final(&out); | 276 | h.update(data); |
| 277 | break :blk try std.fmt.allocPrint(gpa, "{x}", .{out}); | 277 | h.final(&out); |
| 278 | }, | 278 | const hex = try std.fmt.allocPrint(gpa, "{x}", .{out}); |
| 279 | .sha512 => blk: { | 279 | return hex; |
| 280 | const h = &std.crypto.hash.sha2.Sha512.init(.{}); | | |
| 281 | var out: [64]u8 = undefined; | | |
| 282 | h.update(data); | | |
| 283 | h.final(&out); | | |
| 284 | break :blk try std.fmt.allocPrint(gpa, "{x}", .{out}); | | |
| 285 | }, | | |
| 286 | }); | | |
| 287 | } | 280 | } |