v / vlib / x / crypto / ascon / examples / use_of_xof.v
42 lines · 37 sloc · 1.44 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1// Copyright ©2025 blackshirt.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4//
5import encoding.hex
6import x.crypto.ascon
7
8// The material was generated from https://hashing.tools/ascon/ascon-hash
9fn main() {
10 msg := 'Example of Xof128 message'.bytes()
11 // expected output generated from the tool, with 32, 64 dan 75-bytes output
12 digest32 := hex.decode('424caaf68eb94aa251536bb6b565c0695b8944f932d011b1049df85b7f27d2f3')!
13 digest64 :=
14 hex.decode('424caaf68eb94aa251536bb6b565c0695b8944f932d011b1049df85b7f27d2f3bf704643a643c3f2dcfb1e0bc73ec55781b5283966d2d1da85d89794ca5c292e')!
15 digest75 :=
16 hex.decode('424caaf68eb94aa251536bb6b565c0695b8944f932d011b1049df85b7f27d2f3bf704643a643c3f2dcfb1e0bc73ec55781b5283966d2d1da85d89794ca5c292e36260815a8f10088e3804c')!
17
18 out32 := ascon.xof128(msg, 32)!
19 out64 := ascon.xof128(msg, 64)!
20 out75 := ascon.xof128(msg, 75)!
21 dump(out32 == digest32) // out32 == digest32: true
22 dump(out64 == digest64) // out64 == digest64: true
23 dump(out75 == digest75) // out75 == digest75: true
24
25 // With object based
26 mut x := ascon.new_xof128(32)
27 s32 := x.sum(msg)
28 dump(s32 == digest32) // s32 == digest32: true
29
30 // with sized output
31 x.reset()
32 _ := x.write(msg)!
33 mut b64 := []u8{len: 64}
34 _ := x.read(mut b64)!
35 dump(b64 == digest64) // b64 == digest64: true
36
37 x.reset()
38 _ := x.write(msg)!
39 mut b75 := []u8{len: 75}
40 _ := x.read(mut b75)!
41 dump(b75 == digest75) // b75 == digest75: true
42}
43