| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | const rawIntBytes = extras.rawIntBytes; |
| 5 | |
| 6 | pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 { |
| 7 | var h = Algo.init(.{}); |
| 8 | var out: [Algo.digest_length]u8 = undefined; |
| 9 | h.update(bytes); |
| 10 | h.final(&out); |
| 11 | return out; |
| 12 | } |
| 13 | |
| 14 | test { |
| 15 | const A = std.crypto.hash.Md5; |
| 16 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u128, 0x5d41402abc4b2a76b9719d911017c592))); |
| 17 | } |
| 18 | |
| 19 | test { |
| 20 | const A = std.crypto.hash.Sha1; |
| 21 | // by default the array len is 24 for u160 |
| 22 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d))); |
| 23 | } |
| 24 | |
| 25 | test { |
| 26 | const A = std.crypto.hash.sha2.Sha256; |
| 27 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u256, 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824))); |
| 28 | } |