module Cometa.Crypto.Ed25519.Signature
( Signature,
fromBytes,
fromHex,
toBytes,
toHex,
byteLength,
hexLength,
)
where
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)
newtype Signature = Signature ByteString
deriving (Signature -> Signature -> Bool
(Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool) -> Eq Signature
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Signature -> Signature -> Bool
== :: Signature -> Signature -> Bool
$c/= :: Signature -> Signature -> Bool
/= :: Signature -> Signature -> Bool
Eq, Eq Signature
Eq Signature =>
(Signature -> Signature -> Ordering)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Signature)
-> (Signature -> Signature -> Signature)
-> Ord Signature
Signature -> Signature -> Bool
Signature -> Signature -> Ordering
Signature -> Signature -> Signature
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Signature -> Signature -> Ordering
compare :: Signature -> Signature -> Ordering
$c< :: Signature -> Signature -> Bool
< :: Signature -> Signature -> Bool
$c<= :: Signature -> Signature -> Bool
<= :: Signature -> Signature -> Bool
$c> :: Signature -> Signature -> Bool
> :: Signature -> Signature -> Bool
$c>= :: Signature -> Signature -> Bool
>= :: Signature -> Signature -> Bool
$cmax :: Signature -> Signature -> Signature
max :: Signature -> Signature -> Signature
$cmin :: Signature -> Signature -> Signature
min :: Signature -> Signature -> Signature
Ord, Int -> Signature -> ShowS
[Signature] -> ShowS
Signature -> String
(Int -> Signature -> ShowS)
-> (Signature -> String)
-> ([Signature] -> ShowS)
-> Show Signature
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Signature -> ShowS
showsPrec :: Int -> Signature -> ShowS
$cshow :: Signature -> String
show :: Signature -> String
$cshowList :: [Signature] -> ShowS
showList :: [Signature] -> ShowS
Show)
fromBytes :: ByteString -> Either Error Signature
fromBytes :: ByteString -> Either Error Signature
fromBytes ByteString
bytes
| ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
64 = Error -> Either Error Signature
forall a b. a -> Either a b
Left (Int -> Error
InvalidEd25519SignatureLength (ByteString -> Int
ByteString.length ByteString
bytes))
| Bool
otherwise = Signature -> Either Error Signature
forall a b. b -> Either a b
Right (ByteString -> Signature
Signature ByteString
bytes)
fromHex :: Text -> Either Error Signature
fromHex :: Text -> Either Error Signature
fromHex Text
text = do
ByteString
bytes <- Either Error ByteString
-> (ByteString -> Either Error ByteString)
-> Maybe ByteString
-> Either Error ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error ByteString
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (Text -> Maybe ByteString
Hex.decodeHex Text
text)
if ByteString -> Bool
ByteString.null ByteString
bytes then Error -> Either Error Signature
forall a b. a -> Either a b
Left Error
InvalidHex else ByteString -> Either Error Signature
fromBytes ByteString
bytes
toBytes :: Signature -> ByteString
toBytes :: Signature -> ByteString
toBytes (Signature ByteString
bytes) = ByteString
bytes
toHex :: Signature -> Text
toHex :: Signature -> Text
toHex = ByteString -> Text
Hex.encodeHex (ByteString -> Text)
-> (Signature -> ByteString) -> Signature -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> ByteString
toBytes
byteLength :: Signature -> Int
byteLength :: Signature -> Int
byteLength = ByteString -> Int
ByteString.length (ByteString -> Int)
-> (Signature -> ByteString) -> Signature -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> ByteString
toBytes
hexLength :: Signature -> Int
hexLength :: Signature -> Int
hexLength Signature
value = Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Signature -> Int
byteLength Signature
value