{-# OPTIONS_GHC -fno-warn-orphans #-} {-# LANGUAGE OverloadedStrings #-} module Serialization where import Types import Raaz.Core.Encode import qualified Data.ByteString as B import qualified Data.ByteString.Char8 as B8 import Data.Monoid import Data.Word import Text.Read -- TODO -- | An EncryptedSecretKey is serialized as first a md5sum of the rest -- of the content, and then a SelfDescription EncryptedSecretKey, -- and finally the --instance Encodable EncryptedSecretKey where -- toByteString (EncryptedSecretKey b _) = b -- fromByteString b = -- | A KeyIdent is serialized in the form "keytype name". -- For example "gpg Joey Hess" instance Encodable KeyIdent where toByteString (KeyIdent (KeyType t) (Name n)) = t <> B.singleton sepChar <> n fromByteString b = case B.break (== sepChar) b of (t, n) | B.null n -> Nothing | otherwise -> Just $ KeyIdent (KeyType t) (Name (B.drop 1 n)) -- | An ObjectIdent is serialied in the form "shardnum keytype name" -- For example "1 gpg Joey Hess" instance Encodable ObjectIdent where toByteString (ObjectIdent (ShardNum n) keyident) = B8.pack (show n) <> B.singleton sepChar <> toByteString keyident fromByteString b = case B.break (== sepChar) b of (ns, rest) | B.null ns -> Nothing | otherwise -> do keyident <- fromByteString (B.drop 1 rest) n <- readMaybe (B8.unpack ns) return $ ObjectIdent (ShardNum n) keyident sepChar :: Word8 sepChar = 32