From e0762612e95b0816721165c5aba6189809fcde58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Tue, 27 Oct 2026 22:08:47 +0100 Subject: ByteString conversion classes --- src/crypto/reflibs/Crypto/PubKey/Ed25519.hs | 32 +++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src/crypto/reflibs/Crypto/PubKey') diff --git a/src/crypto/reflibs/Crypto/PubKey/Ed25519.hs b/src/crypto/reflibs/Crypto/PubKey/Ed25519.hs index 88c7302..a4c24c0 100644 --- a/src/crypto/reflibs/Crypto/PubKey/Ed25519.hs +++ b/src/crypto/reflibs/Crypto/PubKey/Ed25519.hs @@ -16,10 +16,12 @@ import Crypto.Error import Crypto.Random import Crypto.Sign.Ed25519 qualified as Ed -import Data.ByteArray as BA +import Data.ByteArray qualified as BA import Data.ByteString (ByteString) import Data.ByteString qualified as BS +import ConvertByteString + newtype EdSecretKey = EdSecretKey { fromSecretKey :: Ed.SecretKey } deriving (Eq) @@ -34,16 +36,28 @@ instance Show EdSecretKey where show = show . fromSecretKey instance Show EdPublicKey where show = show . fromPublicKey instance Show EdSignature where show = show . fromSignature -instance ByteArrayAccess EdSecretKey where +instance BA.ByteArrayAccess EdSecretKey where length = BS.length . Ed.openSecretKey . fromSecretKey - withByteArray = withByteArray . Ed.openSecretKey . fromSecretKey + withByteArray = BA.withByteArray . Ed.openSecretKey . fromSecretKey -instance ByteArrayAccess EdPublicKey where +instance BA.ByteArrayAccess EdPublicKey where length = BS.length . Ed.openPublicKey . fromPublicKey - withByteArray = withByteArray . Ed.openPublicKey . fromPublicKey + withByteArray = BA.withByteArray . Ed.openPublicKey . fromPublicKey -instance ByteArrayAccess EdSignature where +instance BA.ByteArrayAccess EdSignature where length = BS.length . Ed.unSignature . fromSignature + withByteArray = BA.withByteArray . Ed.unSignature . fromSignature + +instance ToByteString EdSecretKey where + toByteString = Ed.openSecretKey . fromSecretKey + withByteArray = withByteArray . Ed.openSecretKey . fromSecretKey + +instance ToByteString EdPublicKey where + toByteString = Ed.openPublicKey . fromPublicKey + withByteArray = withByteArray . Ed.openPublicKey . fromPublicKey + +instance ToByteString EdSignature where + toByteString = Ed.unSignature . fromSignature withByteArray = withByteArray . Ed.unSignature . fromSignature signatureSize :: Int @@ -57,16 +71,16 @@ edSignature bs edPublicKey :: ByteString -> CryptoFailable EdPublicKey edPublicKey = maybe (CryptoFailed CryptoError) (CryptoPassed . EdPublicKey) . Ed.makePublicKey . convert -edSecretKey :: ByteArrayAccess ba => ba -> CryptoFailable EdSecretKey +edSecretKey :: ScrubbedBytes -> CryptoFailable EdSecretKey edSecretKey = maybe (CryptoFailed CryptoError) (CryptoPassed . EdSecretKey) . Ed.makeSecretKey . convert edToPublic :: EdSecretKey -> EdPublicKey edToPublic = EdPublicKey . Ed.secretToPublicKey . fromSecretKey -edSign :: ByteArrayAccess ba => EdSecretKey -> EdPublicKey -> ba -> EdSignature +edSign :: ToByteString ba => EdSecretKey -> EdPublicKey -> ba -> EdSignature edSign (EdSecretKey key) _ = EdSignature . Ed.dsign key . convert -edVerify :: ByteArrayAccess ba => EdPublicKey -> ba -> EdSignature -> Bool +edVerify :: ToByteString ba => EdPublicKey -> ba -> EdSignature -> Bool edVerify (EdPublicKey pub) msg (EdSignature sig) = Ed.dverify pub (convert msg) sig edGenerateSecretKey :: IO EdSecretKey -- cgit v1.2.3