Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: add writer methods on all crypto.hash types #10168

Merged
merged 2 commits into from Nov 20, 2021

Conversation

nektro
Copy link
Contributor

@nektro nektro commented Nov 18, 2021

This enables many use cases but the one piqued my interest in this is if you're hashing many files in sequence. since the hash types are able to take in a continuous stream of bytes through .update, one can setup a std.fifo.LinearFifo and hash the files' contents with zero allocations by doing try fifo.pump(file.reader(), hasher.writer());

@nektro
Copy link
Contributor Author

nektro commented Nov 18, 2021

adding this worked too

fn HashWriter(comptime T: type) type {
    return struct {
        h: *T,

        const Self = @This();
        pub const Error = error{};
        pub const Writer = std.io.Writer(*Self, Error, write);

        fn write(self: *Self, bytes: []const u8) Error!usize {
            self.h.update(bytes);
            return bytes.len;
        }

        pub fn writer(self: *Self) Writer {
            return .{ .context = self };
        }
    };
}

@jedisct1
Copy link
Contributor

No love for Gimli and SipHash?

@nektro
Copy link
Contributor Author

nektro commented Nov 18, 2021

oh good catch, I thought I'd added those too

@andrewrk andrewrk merged commit c84147f into ziglang:master Nov 20, 2021
@nektro nektro deleted the meg-hashwriter branch November 22, 2021 00:57
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.

None yet

3 participants