/
hash.zig
49 lines (41 loc) · 1.13 KB
/
hash.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const std = @import("std");
const Sha256 = std.crypto.hash.sha2.Sha256;
pub const HASH_SIZE: usize = 32;
pub const CompareResult = enum {
Greater,
Less,
Equal,
};
pub const Hash = struct {
data: [HASH_SIZE]u8,
const Self = @This();
// used in tests
pub fn random() Self {
var seed = @as(u64, @intCast(std.time.milliTimestamp()));
var rand = std.rand.DefaultPrng.init(seed);
var data: [HASH_SIZE]u8 = undefined;
for (0..HASH_SIZE) |i| {
data[i] = rand.random().int(u8);
}
return Self{
.data = data,
};
}
pub fn generateSha256Hash(bytes: []const u8) Self {
var hash = Hash{
.data = undefined,
};
Sha256.hash(bytes, &hash.data, .{});
return hash;
}
pub fn cmp(a: *const Self, b: *const Self) CompareResult {
for (0..HASH_SIZE) |i| {
if (a.data[i] > b.data[i]) {
return CompareResult.Greater;
} else if (a.data[i] < b.data[i]) {
return CompareResult.Less;
}
}
return CompareResult.Equal;
}
};