module Test.Sodium


import Crypto.Sodium


key : IO Key
key = do k <- newSymmKey
         setKeyChars k 0 [1..26]
         pure k
   where setKeyChars : Key -> Int -> List Int -> IO ()
         setKeyChars k i [] = pure ()
         setKeyChars k i (c :: cs) = do setKeyIdx k i ((c * 42) `mod` 255)
                                        setKeyChars k (i + 1) cs


nonce : IO Nonce
nonce = newNonceFromString "sdlfkjsdlkfjacvbnm,vbnmvhghjvbnasdasdm"


export
test : IO ()
test = do -- first, symmetric key hello world

          encrypted <- cryptoSecretBox "Hello world!" !nonce !key
          print (getBytes encrypted)
          decrypted <- cryptoSecretBoxOpen encrypted !nonce !key

          putStrLn (readBox decrypted)

          --- Now some public/private key stuff

          (alice_pub, alice_sec) <- newKeyPair -- generated by libsodium
          (bob_pub, bob_sec) <- newKeyPair -- generated by libsodium

          encrypted <- cryptoBox "Hello Bob!" !nonce alice_pub bob_sec
          print (getBytes encrypted)
          decrypted <- cryptoBoxOpen encrypted !nonce bob_pub alice_sec

          if (validOpenBox decrypted)
          then putStrLn (readBox decrypted)
          else putStrLn "Error"
