Skip to content

Commit 70c75e4

Browse files
committed
更新
1 parent 5db5a25 commit 70c75e4

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub fn build(b: *std.Build) void {
1717
// zig build [install]
1818
b.installArtifact(unit_tests);
1919

20-
// zig build -Dtest-filter="..." run_unit_test
20+
// zig build -Dtest-filter="..." test
2121
const run_unit_tests = b.addRunArtifact(unit_tests);
22-
const unit_test_step = b.step("unit_test", "Run unit tests");
22+
const unit_test_step = b.step("test", "Run unit tests");
2323
unit_test_step.dependOn(&run_unit_tests.step);
2424
}

src/sm3.zig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ pub const SM3 = struct {
104104
}
105105
}
106106

107+
pub fn finalResult(d: *Self) [digest_length]u8 {
108+
var result: [digest_length]u8 = undefined;
109+
d.final(&result);
110+
return result;
111+
}
112+
107113
fn round(d: *Self, b: *const [64]u8) void {
108114
var a: [8]u32 = undefined;
109115
var w: [68]u32 = undefined;
@@ -207,6 +213,18 @@ pub const SM3 = struct {
207213
return ((y ^ z) & x) ^ z;
208214
}
209215

216+
pub const Error = error{};
217+
pub const Writer = std.io.Writer(*Self, Error, write);
218+
219+
fn write(self: *Self, bytes: []const u8) Error!usize {
220+
self.update(bytes);
221+
return bytes.len;
222+
}
223+
224+
pub fn writer(self: *Self) Writer {
225+
return .{ .context = self };
226+
}
227+
210228
};
211229

212230
// Hash using the specified hasher `H` asserting `expected == H(input)`.
@@ -258,6 +276,24 @@ test "streaming" {
258276
try assertEqual("66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", out[0..]);
259277
}
260278

279+
test "finalResult" {
280+
var h = SM3.init(.{});
281+
var out = h.finalResult();
282+
try assertEqual("1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b", out[0..]);
283+
284+
h = SM3.init(.{});
285+
h.update("abc");
286+
out = h.finalResult();
287+
try assertEqual("66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", out[0..]);
288+
}
289+
290+
test "writer" {
291+
var h = SM3.init(.{});
292+
try h.writer().print("{s}", .{"abc"});
293+
const out = h.finalResult();
294+
try assertEqual("66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", out[0..]);
295+
}
296+
261297
test "aligned final" {
262298
var block = [_]u8{0} ** SM3.block_length;
263299
var out: [SM3.digest_length]u8 = undefined;

0 commit comments

Comments
 (0)