From d6e03020851601668cf5419950ca0cc49e54c42f Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Mon, 15 Jul 2019 18:49:22 -0400 Subject: Switched fingerprint to a wrapped ByteString. --- Data/OpenPGP/Util/Base.hs | 2 +- Data/OpenPGP/Util/Fingerprint.hs | 37 +++++++++++++++++++++---------------- Data/OpenPGP/Util/Sign.hs | 2 +- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Data/OpenPGP/Util/Base.hs b/Data/OpenPGP/Util/Base.hs index c1088d8..5b3e159 100644 --- a/Data/OpenPGP/Util/Base.hs +++ b/Data/OpenPGP/Util/Base.hs @@ -82,7 +82,7 @@ toLazyBS :: BS.ByteString -> LZ.ByteString toLazyBS = LZ.fromChunks . (:[]) find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet -find_key = OpenPGP.find_key fingerprint +find_key = OpenPGP.find_key (show . fingerprint) diff --git a/Data/OpenPGP/Util/Fingerprint.hs b/Data/OpenPGP/Util/Fingerprint.hs index 20b6e72..b2e3904 100644 --- a/Data/OpenPGP/Util/Fingerprint.hs +++ b/Data/OpenPGP/Util/Fingerprint.hs @@ -1,5 +1,5 @@ {-# LANGUAGE CPP #-} -module Data.OpenPGP.Util.Fingerprint (fingerprint) where +module Data.OpenPGP.Util.Fingerprint (fingerprint,Fingerprint(..)) where import qualified Data.OpenPGP as OpenPGP import qualified Data.ByteString as BS @@ -17,12 +17,29 @@ import Crypto.Hash.MD5 as MD5 import Crypto.Hash.SHA1 as SHA1 #endif +oo :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c +oo = (.) . (.) + +newtype Fingerprint = Fingerprint BS.ByteString + +instance Show Fingerprint where + show (Fingerprint bs) = hexify bs + where + hexify = map toUpper . hexString . BS.unpack + + hexString :: [Word8] -> String + hexString = foldr (pad `oo` showHex) "" + where + pad s | odd $ length s = '0':s + | otherwise = s + + -- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket -- -fingerprint :: OpenPGP.Packet -> String +fingerprint :: OpenPGP.Packet -> Fingerprint fingerprint p - | OpenPGP.version p == 4 = hexify $ sha1 material - | OpenPGP.version p `elem` [2, 3] = hexify $ md5 material + | OpenPGP.version p == 4 = Fingerprint $ sha1 material + | OpenPGP.version p `elem` [2, 3] = Fingerprint $ md5 material | otherwise = error "Unsupported Packet version or type in fingerprint" where @@ -35,15 +52,3 @@ fingerprint p #endif material = LZ.concat $ OpenPGP.fingerprint_material p - - hexify = map toUpper . hexString . BS.unpack - - hexString :: [Word8] -> String - hexString = foldr (pad `oo` showHex) "" - where - pad s | odd $ length s = '0':s - | otherwise = s - - oo :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c - oo = (.) . (.) - diff --git a/Data/OpenPGP/Util/Sign.hs b/Data/OpenPGP/Util/Sign.hs index 8663a0d..d96c3a7 100644 --- a/Data/OpenPGP/Util/Sign.hs +++ b/Data/OpenPGP/Util/Sign.hs @@ -115,7 +115,7 @@ unsafeSign keys over hsh keyid timestamp g = (over {OpenPGP.signatures_over = [s ([ -- Do we really need to pass in timestamp just for the default? OpenPGP.SignatureCreationTimePacket $ fromIntegral timestamp, - OpenPGP.IssuerPacket $ fingerprint k + OpenPGP.IssuerPacket $ show $ fingerprint k ] ++ (case over of OpenPGP.KeySignature {} -> [OpenPGP.KeyFlagsPacket { OpenPGP.certify_keys = True, -- cgit v1.2.3