{-# LANGUAGE Safe #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Crypto.Sign.Ed25519
(
PublicKey(..)
, SecretKey(..)
, createKeypair
, createKeypairFromSeed_
, createKeypairFromSeed
, toPublicKey
, sign
, verify
, Signature(..)
, dsign
, dverify
, sign'
, verify'
) where
import safe Prelude (Eq,Show, Ord, IO, Either(Right,Left), Maybe, Bool, return, undefined, error, (==))
import safe GHC.Generics (Generic)
import safe qualified Crypto.ECC.Ed25519.Sign as S
import safe qualified Crypto.ECC.Ed25519.Internal.Ed25519 as I
import safe qualified Data.ByteString as BS
newtype PublicKey = PublicKey { unPublicKey :: BS.ByteString
}
deriving (Eq, Show, Ord, Generic)
newtype SecretKey = SecretKey { unSecretKey :: BS.ByteString
}
deriving (Eq, Show, Ord, Generic)
createKeypair :: IO (PublicKey, SecretKey)
createKeypair = do
a <- S.genkeys
case a of
Right (I.SecKeyBytes sk, pk) -> return (PublicKey pk, SecretKey sk)
Left e -> error e
createKeypairFromSeed_ :: BS.ByteString -> Maybe (PublicKey, SecretKey)
createKeypairFromSeed_ = undefined
createKeypairFromSeed :: BS.ByteString -> (PublicKey, SecretKey)
createKeypairFromSeed = undefined
toPublicKey :: SecretKey -> PublicKey
toPublicKey sk = let (SecretKey sk') = sk
sk'' = I.SecKeyBytes sk'
a = S.publickey sk''
in case a of
Right pk -> PublicKey pk
Left e -> error e
sign :: SecretKey -> BS.ByteString -> BS.ByteString
sign sk m = let SecretKey sk' = sk
a = S.sign (I.SecKeyBytes sk') m
in case a of
Right sigm -> sigm
Left e -> error e
verify :: PublicKey -> BS.ByteString -> Bool
verify pk m = let PublicKey pk' = pk
in S.verify pk' m == Right I.SigOK
newtype Signature = Signature { unSignature :: BS.ByteString
}
deriving (Eq, Show, Ord)
dsign :: SecretKey -> BS.ByteString -> Signature
dsign sk m = let SecretKey sk' = sk
a = S.dsign (I.SecKeyBytes sk') m
in case a of
Right sig -> Signature sig
Left e -> error e
dverify :: PublicKey -> BS.ByteString -> Signature -> Bool
dverify pk m sig = let PublicKey pk' = pk
Signature sig' = sig
in S.dverify pk' sig' m == Right I.SigOK
sign' :: SecretKey -> BS.ByteString -> Signature
sign' sk m = dsign sk m
verify' :: PublicKey -> BS.ByteString -> Signature -> Bool
verify' pk m sig = dverify pk m sig