2017-03-01 13:04:27 -05:00
|
|
|
! Copyright (C) 2017 Alexander Ilin.
|
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2017-03-01 15:45:17 -05:00
|
|
|
USING: byte-arrays init io.encodings.string io.encodings.utf8
|
|
|
|
|
kernel math sequences sodium.ffi ;
|
2017-03-01 13:04:27 -05:00
|
|
|
IN: sodium
|
|
|
|
|
|
|
|
|
|
ERROR: sodium-init-fail ;
|
2017-03-01 15:45:17 -05:00
|
|
|
ERROR: call-fail ;
|
2017-03-01 13:04:27 -05:00
|
|
|
|
|
|
|
|
! Call this before any other function, may be called multiple times.
|
|
|
|
|
: sodium-init ( -- ) sodium_init 0 < [ sodium-init-fail ] when ;
|
|
|
|
|
|
2017-03-01 13:29:32 -05:00
|
|
|
: random-bytes ( byte-array -- byte-array' )
|
|
|
|
|
dup dup length randombytes_buf ;
|
|
|
|
|
|
2017-03-01 15:45:17 -05:00
|
|
|
: n-random-bytes ( n -- byte-array )
|
|
|
|
|
<byte-array> random-bytes ;
|
|
|
|
|
|
|
|
|
|
: check0 ( n -- ) 0 = [ call-fail ] unless ;
|
|
|
|
|
|
|
|
|
|
: crypto-pwhash-str ( password opslimit memlimit -- str )
|
|
|
|
|
[ crypto_pwhash_strbytes <byte-array> dup ] 3dip
|
|
|
|
|
[ utf8 encode dup length ] 2dip crypto_pwhash_str check0
|
|
|
|
|
utf8 decode ;
|
|
|
|
|
|
|
|
|
|
: crypto-pwhash-str-verify ( str password -- bool )
|
|
|
|
|
[ utf8 encode ] bi@ dup length crypto_pwhash_str_verify 0 = ;
|
|
|
|
|
|
2017-03-01 17:04:19 -05:00
|
|
|
: crypto-generichash ( out-bytes in-bytes key-bytes/f -- out-bytes' )
|
|
|
|
|
[ dup ] 2dip [ dup length ] tri@ crypto_generichash check0 ;
|
|
|
|
|
|
2017-03-01 13:04:27 -05:00
|
|
|
[ sodium-init ] "sodium" add-startup-hook
|