sodium: the add essential crypto-box-* words and a unit-test
parent
0f57380074
commit
2f605617a1
|
@ -0,0 +1,12 @@
|
|||
! Copyright (C) 2018 Alexander Ilin.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: arrays io.encodings.string io.encodings.utf8 kernel math
|
||||
sequences sodium tools.test ;
|
||||
IN: sodium.tests
|
||||
|
||||
{ t } [
|
||||
"Encrypted message" dup utf8 encode
|
||||
crypto-box-nonce 2 [ crypto-box-keypair 2array ] times
|
||||
[ [ first ] [ second ] bi* crypto-box-easy ] 3keep swap
|
||||
[ first ] [ second ] bi* crypto-box-open-easy utf8 decode =
|
||||
] unit-test
|
|
@ -25,6 +25,11 @@ ERROR: buffer-too-small ;
|
|||
: secretbox-message-buf ( cipher-length -- byte-array )
|
||||
crypto_secretbox_macbytes message-buf ;
|
||||
|
||||
: box-cipher-buf ( message-length -- byte-array )
|
||||
crypto_box_macbytes cipher-buf ;
|
||||
|
||||
: box-message-buf ( cipher-length -- byte-array )
|
||||
crypto_box_macbytes message-buf ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
|
@ -65,4 +70,22 @@ PRIVATE>
|
|||
[ crypto_secretbox_keybytes check-length ] tri*
|
||||
crypto_secretbox_open_easy 0 = [ drop f ] unless ;
|
||||
|
||||
: crypto-box-keypair ( -- public-key secret-key )
|
||||
crypto_box_publickeybytes <byte-array>
|
||||
crypto_box_secretkeybytes <byte-array>
|
||||
2dup crypto_box_keypair check0 ;
|
||||
|
||||
: crypto-box-nonce ( -- nonce-bytes )
|
||||
crypto_box_noncebytes n-random-bytes ;
|
||||
|
||||
: crypto-box-easy ( message nonce public-key private-key -- cipher-bytes )
|
||||
[
|
||||
dup length [ box-cipher-buf dup rot ] keep
|
||||
] 3dip crypto_box_easy check0 ;
|
||||
|
||||
: crypto-box-open-easy ( cipher-bytes nonce public-key private-key -- message )
|
||||
[
|
||||
dup length [ box-message-buf dup rot ] keep
|
||||
] 3dip crypto_box_open_easy check0 ;
|
||||
|
||||
[ sodium-init ] "sodium" add-startup-hook
|
||||
|
|
Loading…
Reference in New Issue