@@ -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+
261297test "aligned final" {
262298 var block = [_ ]u8 {0 } ** SM3 .block_length ;
263299 var out : [SM3 .digest_length ]u8 = undefined ;
0 commit comments