Skip to content

Conversation

@manoahLinks
Copy link

No description provided.


const result = engine.final();

try std.testing.expect(std.mem.eql(u8, result, expected_hash));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u can use here std.testing.expectEqualSlices

@@ -0,0 +1,56 @@
const std = @import("std");
const Hash = std.crypto.sha256;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it okay? std.crypto.sha256 is located in std.crypto.hash.sha2.Sha256

@StringNick
Copy link
Collaborator

also u need import here sha256d, currently zig build test dont see ur test

pub const Ripemd160 = @import("ripemd160.zig").Ripemd160;

Comment on lines 8 to 10
return HashEngine{
.sha256_engine = Hash.Context.init(),
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return HashEngine{
.sha256_engine = Hash.Context.init(),
};
return .{
.sha256_engine = Hash.Context.init(),
};

const std = @import("std");
const Hash = std.crypto.sha256;

pub const HashEngine = struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub const HashEngine = struct {
pub const HashEngine = struct {
const Self = @This();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will also suggest we rename this from HashEngine to Sha256d

pub const HashEngine = struct {
sha256_engine: Hash.Context,

pub fn new() HashEngine {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn new() HashEngine {
pub fn init() Self {

};
}

pub fn input(self: *HashEngine, data: []const u8) void {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn final(self: *HashEngine) []u8 {
pub fn final(self: *Self) []u8 {


const result = engine.final();

try std.testing.expect(std.mem.eql(u8, result, expected_hash));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expectEqualSlices is too

return self.sha256_engine.total_len;
}

pub fn final(self: *HashEngine) []u8 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here why can't you return a [32]u8 as you know the size of the output at compile time no?

sha2_engine.update(&sha1_result);

var sha2_result: [32]u8 = undefined;
sha2_engine.final(&sha2_result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we reuse here sha1_result, and not create a second buffer for output?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants