{-# OPTIONS_HADDOCK not-home #-}
module Crypto.Sign
(
PublicKey
, toPublicKey
, SecretKey
, toSecretKey
, keypair
, keypairFromSeed
, unsafeKeypairFromSeed
, create
, open
) where
import Data.ByteArray (ByteArrayAccess, ScrubbedBytes, withByteArray)
import Data.ByteString (ByteString)
import Data.ByteArray.Sized (SizedByteArray, alloc, allocRet)
import Data.Functor (void)
import Data.Proxy (Proxy(..))
import System.IO.Unsafe (unsafePerformIO)
import qualified Libsodium as Na
import NaCl.Sign
(PublicKey, SecretKey, create, keypair, open, toPublicKey, toSecretKey)
type Seed a = SizedByteArray Na.CRYPTO_SIGN_SEEDBYTES a
keypairFromSeed
:: ByteArrayAccess seed
=> Seed seed
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
keypairFromSeed :: Seed seed -> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
keypairFromSeed Seed seed
seed = do
Proxy 64
-> (Ptr CUChar -> IO (PublicKey ByteString))
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
forall (n :: Nat) c p a.
ByteArrayN n c =>
Proxy n -> (Ptr p -> IO a) -> IO (a, c)
allocRet Proxy 64
forall k (t :: k). Proxy t
Proxy ((Ptr CUChar -> IO (PublicKey ByteString))
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes))
-> (Ptr CUChar -> IO (PublicKey ByteString))
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
skPtr ->
(Ptr CUChar -> IO ()) -> IO (PublicKey ByteString)
forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
alloc ((Ptr CUChar -> IO ()) -> IO (PublicKey ByteString))
-> (Ptr CUChar -> IO ()) -> IO (PublicKey ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
pkPtr ->
Seed seed -> (Ptr CUChar -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Seed seed
seed ((Ptr CUChar -> IO ()) -> IO ()) -> (Ptr CUChar -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
sdPtr ->
IO CInt -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr CUChar -> Ptr CUChar -> Ptr CUChar -> IO CInt
forall k1 k2 k3 (pk :: k1) (sk :: k2) (seed :: k3).
Ptr CUChar -> Ptr CUChar -> Ptr CUChar -> IO CInt
Na.crypto_sign_seed_keypair Ptr CUChar
pkPtr Ptr CUChar
skPtr Ptr CUChar
sdPtr
unsafeKeypairFromSeed
:: ByteArrayAccess seed
=> Seed seed
-> (PublicKey ByteString, SecretKey ScrubbedBytes)
unsafeKeypairFromSeed :: Seed seed -> (PublicKey ByteString, SecretKey ScrubbedBytes)
unsafeKeypairFromSeed = IO (PublicKey ByteString, SecretKey ScrubbedBytes)
-> (PublicKey ByteString, SecretKey ScrubbedBytes)
forall a. IO a -> a
unsafePerformIO (IO (PublicKey ByteString, SecretKey ScrubbedBytes)
-> (PublicKey ByteString, SecretKey ScrubbedBytes))
-> (Seed seed
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes))
-> Seed seed
-> (PublicKey ByteString, SecretKey ScrubbedBytes)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seed seed -> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
forall seed.
ByteArrayAccess seed =>
Seed seed -> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
keypairFromSeed