-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(libutil): implement hashing in zig
- Loading branch information
1 parent
4d8b5d4
commit 0866d82
Showing
6 changed files
with
115 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const std = @import("std"); | ||
|
||
pub const HashAlgorithm = enum(c_char) { | ||
md5 = 42, | ||
sha1, | ||
sha256, | ||
sha512, | ||
blake3, | ||
}; | ||
|
||
pub const Ctx = union(HashAlgorithm) { | ||
md5: std.crypto.hash.Md5, | ||
sha1: std.crypto.hash.Sha1, | ||
sha256: std.crypto.hash.sha2.Sha256, | ||
sha512: std.crypto.hash.sha2.Sha512, | ||
blake3: std.crypto.hash.Blake3, | ||
|
||
pub const Extern = opaque {}; | ||
|
||
pub inline fn toExtern(self: *Ctx) *Extern { | ||
return @ptrCast(@alignCast(self)); | ||
} | ||
|
||
pub inline fn fromExtern(e: *Extern) *Ctx { | ||
return @ptrCast(@alignCast(e)); | ||
} | ||
}; | ||
|
||
const sizes = struct { | ||
pub const md5 = 16; | ||
pub const blake3 = 64; | ||
pub const sha1 = 20; | ||
pub const sha256 = 32; | ||
pub const sha512 = 64; | ||
}; | ||
|
||
pub fn create(t: HashAlgorithm) callconv(.C) *Ctx.Extern { | ||
const self = std.heap.c_allocator.create(Ctx) catch @panic("OOM"); | ||
inline for (comptime std.meta.fields(Ctx)) |field| { | ||
const v = comptime std.meta.stringToEnum(HashAlgorithm, field.name) orelse unreachable; | ||
if (v == t) { | ||
self.* = @unionInit(Ctx, field.name, field.type.init(.{})); | ||
return self.toExtern(); | ||
} | ||
} | ||
unreachable; | ||
} | ||
|
||
pub fn dupe(e: *Ctx.Extern) callconv(.C) *Ctx.Extern { | ||
const self = Ctx.fromExtern(e); | ||
const self2 = std.heap.c_allocator.dupe(*Ctx, &.{self}) catch @panic("OOM"); | ||
return self2[0].toExtern(); | ||
} | ||
|
||
pub fn update(e: *Ctx.Extern, raw_data: [*]const u8, size: usize) callconv(.C) void { | ||
const self = Ctx.fromExtern(e); | ||
|
||
const data = raw_data[0..size]; | ||
|
||
inline for (comptime std.meta.fields(Ctx)) |field| { | ||
const v = comptime std.meta.stringToEnum(HashAlgorithm, field.name) orelse unreachable; | ||
if (self.* == v) { | ||
@field(self, field.name).update(data); | ||
return; | ||
} | ||
} | ||
|
||
unreachable; | ||
} | ||
|
||
pub fn finish(e: *Ctx.Extern, raw_hash: [*:0]u8) callconv(.C) void { | ||
const self = Ctx.fromExtern(e); | ||
|
||
inline for (comptime std.meta.fields(Ctx)) |field| { | ||
const v = comptime std.meta.stringToEnum(HashAlgorithm, field.name) orelse unreachable; | ||
const size = @field(sizes, field.name); | ||
if (self.* == v) { | ||
std.debug.assert(size <= 64); | ||
const hash = raw_hash[0..size]; | ||
@field(self, field.name).final(hash[0..size]); | ||
@memset(raw_hash[size..64], 0); | ||
return; | ||
} | ||
} | ||
|
||
unreachable; | ||
} | ||
|
||
comptime { | ||
@export(&create, .{ .name = "nix_libutil_hash_create" }); | ||
@export(&dupe, .{ .name = "nix_libutil_hash_dupe" }); | ||
@export(&update, .{ .name = "nix_libutil_hash_update" }); | ||
@export(&finish, .{ .name = "nix_libutil_hash_finish" }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub const cpuid = @import("cpuid.zig"); | ||
pub const hash = @import("hash.zig"); | ||
|
||
comptime { | ||
_ = cpuid; | ||
_ = hash; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters