From 501399ab698472715e9e65da0efca6cf37644fab Mon Sep 17 00:00:00 2001 From: manoahLinks Date: Tue, 10 Sep 2024 13:46:56 +0100 Subject: [PATCH 1/4] Feat/Hashers: Implement sha256d :fixes #3 --- src/hashes/sha256d.zig | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/hashes/sha256d.zig diff --git a/src/hashes/sha256d.zig b/src/hashes/sha256d.zig new file mode 100644 index 0000000..59fa6fc --- /dev/null +++ b/src/hashes/sha256d.zig @@ -0,0 +1,56 @@ +const std = @import("std"); +const Hash = std.crypto.sha256; + +pub const HashEngine = struct { + sha256_engine: Hash.Context, + + pub fn new() HashEngine { + return HashEngine{ + .sha256_engine = Hash.Context.init(), + }; + } + + pub fn input(self: *HashEngine, data: []const u8) void { + self.sha256_engine.update(data); + } + + pub fn n_bytes_hashed(self: *const HashEngine) usize { + return self.sha256_engine.total_len; + } + + pub fn final(self: *HashEngine) []u8 { + var sha1_result: [32]u8 = undefined; + self.sha256_engine.final(&sha1_result); + + var sha2_result: [32]u8 = undefined; + var second_sha256 = Hash.hash(sha1_result[0..]); + std.mem.copy(u8, sha2_result[0..], second_sha256[0..]); + + return sha2_result[0..]; + } +}; + +// Test cases +test "double SHA256 (SHA256d)" { + const input = "hello world"; + var engine = HashEngine.new(); + engine.input(input); + + const expected_hash: [32]u8 = [_]u8{ 0xb9, 0x4d, 0x27, 0xb9, 0x93, 0x4d, 0x3e, 0x08, 0xa5, 0x2e, 0x52, 0xd7, 0xda, 0x7d, 0xab, 0xfa, 0xc4, 0x6e, 0x0f, 0xf4, 0x78, 0x91, 0x87, 0x3c, 0xc8, 0x89, 0x3f, 0xbe, 0x58, 0x68, 0xb2, 0x38 }; + + const result = engine.final(); + + try std.testing.expect(std.mem.eql(u8, result, expected_hash)); +} + +test "empty string double SHA256 (SHA256d)" { + const input = ""; + var engine = HashEngine.new(); + engine.input(input); + + const expected_hash: [32]u8 = [_]u8{ 0x5d, 0xf6, 0xe0, 0xe2, 0x76, 0x13, 0x59, 0xd3, 0x0a, 0x82, 0x75, 0x05, 0x8e, 0x29, 0x9f, 0xcc, 0x03, 0x81, 0x53, 0x45, 0x45, 0xf5, 0x5c, 0xf4, 0x3e, 0x41, 0x98, 0x3f, 0x5d, 0x4c, 0x94, 0x56 }; + + const result = engine.final(); + + try std.testing.expect(std.mem.eql(u8, result, expected_hash)); +} From 69a2b6324b4b67ef42e3a40b538daeefae262ddf Mon Sep 17 00:00:00 2001 From: manoahLinks Date: Thu, 19 Sep 2024 06:19:13 +0100 Subject: [PATCH 2/4] Feat/Hashers: add sha256d --- src/hashes/lib.zig | 1 + src/hashes/sha256d.zig | 42 ++++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/hashes/lib.zig b/src/hashes/lib.zig index 20f5bc4..0d9f482 100644 --- a/src/hashes/lib.zig +++ b/src/hashes/lib.zig @@ -1 +1,2 @@ pub const Ripemd160 = @import("ripemd160.zig").Ripemd160; +pub const Sha256d = @import("sha256d.zig").Sha256d; diff --git a/src/hashes/sha256d.zig b/src/hashes/sha256d.zig index 59fa6fc..5e029cc 100644 --- a/src/hashes/sha256d.zig +++ b/src/hashes/sha256d.zig @@ -1,56 +1,62 @@ const std = @import("std"); -const Hash = std.crypto.sha256; +const Hash = std.crypto.hash.sha2.Sha256; +const testing = std.testing; -pub const HashEngine = struct { - sha256_engine: Hash.Context, +pub const Sha256d = struct { + const Self = @This(); - pub fn new() HashEngine { - return HashEngine{ - .sha256_engine = Hash.Context.init(), + sha256_engine: Hash, + + pub fn init() Self { + return Sha256d{ + .sha256_engine = Hash.init(), }; } - pub fn input(self: *HashEngine, data: []const u8) void { + pub fn input(self: *Self, data: []const u8) void { self.sha256_engine.update(data); } - pub fn n_bytes_hashed(self: *const HashEngine) usize { + pub fn n_bytes_hashed(self: *const Self) usize { return self.sha256_engine.total_len; } - pub fn final(self: *HashEngine) []u8 { + pub fn final(self: *Self) [32]u8 { var sha1_result: [32]u8 = undefined; + self.sha256_engine.final(&sha1_result); + var sha2_engine = Hash.init(); + sha2_engine.update(&sha1_result); + var sha2_result: [32]u8 = undefined; - var second_sha256 = Hash.hash(sha1_result[0..]); - std.mem.copy(u8, sha2_result[0..], second_sha256[0..]); + sha2_engine.final(&sha2_result); - return sha2_result[0..]; + return sha2_result; } }; // Test cases test "double SHA256 (SHA256d)" { - const input = "hello world"; - var engine = HashEngine.new(); + const input = "hello world"[0..11]; + var engine = Sha256d.init(); engine.input(input); const expected_hash: [32]u8 = [_]u8{ 0xb9, 0x4d, 0x27, 0xb9, 0x93, 0x4d, 0x3e, 0x08, 0xa5, 0x2e, 0x52, 0xd7, 0xda, 0x7d, 0xab, 0xfa, 0xc4, 0x6e, 0x0f, 0xf4, 0x78, 0x91, 0x87, 0x3c, 0xc8, 0x89, 0x3f, 0xbe, 0x58, 0x68, 0xb2, 0x38 }; const result = engine.final(); - try std.testing.expect(std.mem.eql(u8, result, expected_hash)); + try std.testing.expect(std.mem.eql(u8, result[0..], &expected_hash)); } test "empty string double SHA256 (SHA256d)" { - const input = ""; - var engine = HashEngine.new(); + const input = ""[0..]; + var engine = Sha256d.init(); engine.input(input); const expected_hash: [32]u8 = [_]u8{ 0x5d, 0xf6, 0xe0, 0xe2, 0x76, 0x13, 0x59, 0xd3, 0x0a, 0x82, 0x75, 0x05, 0x8e, 0x29, 0x9f, 0xcc, 0x03, 0x81, 0x53, 0x45, 0x45, 0xf5, 0x5c, 0xf4, 0x3e, 0x41, 0x98, 0x3f, 0x5d, 0x4c, 0x94, 0x56 }; const result = engine.final(); - try std.testing.expect(std.mem.eql(u8, result, expected_hash)); + try testing.expect(std.mem.eql(u8, result[0..], &expected_hash)); } From e59d4f2cdba6e1ee11e8b30159448632715bc556 Mon Sep 17 00:00:00 2001 From: manoahLinks Date: Sat, 21 Sep 2024 15:24:42 +0100 Subject: [PATCH 3/4] feat: add sha256d --- src/hashes/sha256d.zig | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/hashes/sha256d.zig b/src/hashes/sha256d.zig index 5e029cc..f5c0d7d 100644 --- a/src/hashes/sha256d.zig +++ b/src/hashes/sha256d.zig @@ -21,18 +21,15 @@ pub const Sha256d = struct { return self.sha256_engine.total_len; } - pub fn final(self: *Self) [32]u8 { + pub fn final(self: *Self) []u8 { var sha1_result: [32]u8 = undefined; - self.sha256_engine.final(&sha1_result); - var sha2_engine = Hash.init(); - sha2_engine.update(&sha1_result); - var sha2_result: [32]u8 = undefined; - sha2_engine.final(&sha2_result); + var second_sha256 = Hash.hash(sha1_result[0..]); + std.mem.copy(u8, sha2_result[0..], second_sha256[0..]); - return sha2_result; + return sha2_result[0..]; } }; From 87acf2ae20f1ee9122dd8b79c48f6b26a919cad0 Mon Sep 17 00:00:00 2001 From: manoahLinks Date: Sat, 21 Sep 2024 15:38:51 +0100 Subject: [PATCH 4/4] feat: add sha256d --- src/hashes/sha256d.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hashes/sha256d.zig b/src/hashes/sha256d.zig index f5c0d7d..11559b8 100644 --- a/src/hashes/sha256d.zig +++ b/src/hashes/sha256d.zig @@ -21,15 +21,15 @@ pub const Sha256d = struct { return self.sha256_engine.total_len; } - pub fn final(self: *Self) []u8 { + pub fn final(self: *Self) [32]u8 { var sha1_result: [32]u8 = undefined; self.sha256_engine.final(&sha1_result); - var sha2_result: [32]u8 = undefined; - var second_sha256 = Hash.hash(sha1_result[0..]); - std.mem.copy(u8, sha2_result[0..], second_sha256[0..]); + var sha2_engine = Hash.init(); + sha2_engine.update(&sha1_result); + sha2_engine.final(&sha1_result); - return sha2_result[0..]; + return sha1_result; } };