summaryrefslogtreecommitdiff
path: root/Data/OpenPGP/Util/Fingerprint.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Data/OpenPGP/Util/Fingerprint.hs')
-rw-r--r--Data/OpenPGP/Util/Fingerprint.hs20
1 files changed, 18 insertions, 2 deletions
diff --git a/Data/OpenPGP/Util/Fingerprint.hs b/Data/OpenPGP/Util/Fingerprint.hs
index 538688b..20b6e72 100644
--- a/Data/OpenPGP/Util/Fingerprint.hs
+++ b/Data/OpenPGP/Util/Fingerprint.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1module Data.OpenPGP.Util.Fingerprint (fingerprint) where 2module Data.OpenPGP.Util.Fingerprint (fingerprint) where
2 3
3import qualified Data.OpenPGP as OpenPGP 4import qualified Data.OpenPGP as OpenPGP
@@ -7,17 +8,32 @@ import Data.Char (toUpper)
7import Data.Word (Word8) 8import Data.Word (Word8)
8import Numeric (showHex) 9import Numeric (showHex)
9 10
11#if defined(VERSION_cryptonite)
12import Crypto.Hash.Algorithms
13import Crypto.Hash
14import qualified Data.ByteArray as Bytes
15#else
10import Crypto.Hash.MD5 as MD5 16import Crypto.Hash.MD5 as MD5
11import Crypto.Hash.SHA1 as SHA1 17import Crypto.Hash.SHA1 as SHA1
18#endif
12 19
13-- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket 20-- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket
14-- <http://tools.ietf.org/html/rfc4880#section-12.2> 21-- <http://tools.ietf.org/html/rfc4880#section-12.2>
15fingerprint :: OpenPGP.Packet -> String 22fingerprint :: OpenPGP.Packet -> String
16fingerprint p 23fingerprint p
17 | OpenPGP.version p == 4 = hexify $ SHA1.hashlazy material 24 | OpenPGP.version p == 4 = hexify $ sha1 material
18 | OpenPGP.version p `elem` [2, 3] = hexify $ MD5.hashlazy material 25 | OpenPGP.version p `elem` [2, 3] = hexify $ md5 material
19 | otherwise = error "Unsupported Packet version or type in fingerprint" 26 | otherwise = error "Unsupported Packet version or type in fingerprint"
20 where 27 where
28
29#if defined(VERSION_cryptonite)
30 sha1 x = Bytes.convert (hashlazy x :: Digest SHA1)
31 md5 x = Bytes.convert (hashlazy x :: Digest MD5)
32#else
33 sha1 = SHA1.hashlazy
34 md5 = MD5.hashlazy
35#endif
36
21 material = LZ.concat $ OpenPGP.fingerprint_material p 37 material = LZ.concat $ OpenPGP.fingerprint_material p
22 38
23 hexify = map toUpper . hexString . BS.unpack 39 hexify = map toUpper . hexString . BS.unpack