1 ! Copyright (C) 2017 Alexander Ilin.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien byte-arrays checksums checksums.common
4 destructors kernel math sequences sodium sodium.ffi ;
9 { key maybe{ byte-array } } ;
11 INSTANCE: sodium-checksum block-checksum
12 C: <sodium-checksum> sodium-checksum
16 TUPLE: sodium-state < disposable
18 { output-size fixnum }
19 { output maybe{ byte-array } } ;
23 M: sodium-checksum initialize-checksum-state
24 [ key>> ] [ output-size>> ] bi
25 sodium-state new-disposable swap >>output-size
26 crypto_generichash_statebytes sodium_malloc >>state
28 [ state>> ] [ drop swap dup length ] [ output-size>> ] tri
29 crypto_generichash_init check0
32 M: sodium-state dispose*
33 state>> [ sodium_free ] when* ;
35 M: sodium-state add-checksum-bytes
36 [ dup state>> ] dip dup length crypto_generichash_update check0 ;
38 M: sodium-state get-checksum
41 over output-size>> [ <byte-array> ] keep
42 [ crypto_generichash_final check0 ] keepd