summaryrefslogtreecommitdiff
path: root/Data/OpenPGP/Util/Fingerprint.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2013-12-14 22:34:26 -0500
committerjoe <joe@jerkface.net>2013-12-14 22:34:26 -0500
commitce0d32ef83ccf15198bdd5248faa02abbcf2f769 (patch)
tree96155973d7af58bbb79a8f67a21f646af46cb47f /Data/OpenPGP/Util/Fingerprint.hs
New package openpgp-util as alternative to OpenPGP-CryptoAPI.
Diffstat (limited to 'Data/OpenPGP/Util/Fingerprint.hs')
-rw-r--r--Data/OpenPGP/Util/Fingerprint.hs33
1 files changed, 33 insertions, 0 deletions
diff --git a/Data/OpenPGP/Util/Fingerprint.hs b/Data/OpenPGP/Util/Fingerprint.hs
new file mode 100644
index 0000000..538688b
--- /dev/null
+++ b/Data/OpenPGP/Util/Fingerprint.hs
@@ -0,0 +1,33 @@
1module Data.OpenPGP.Util.Fingerprint (fingerprint) where
2
3import qualified Data.OpenPGP as OpenPGP
4import qualified Data.ByteString as BS
5import qualified Data.ByteString.Lazy as LZ
6import Data.Char (toUpper)
7import Data.Word (Word8)
8import Numeric (showHex)
9
10import Crypto.Hash.MD5 as MD5
11import Crypto.Hash.SHA1 as SHA1
12
13-- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket
14-- <http://tools.ietf.org/html/rfc4880#section-12.2>
15fingerprint :: OpenPGP.Packet -> String
16fingerprint p
17 | OpenPGP.version p == 4 = hexify $ SHA1.hashlazy material
18 | OpenPGP.version p `elem` [2, 3] = hexify $ MD5.hashlazy material
19 | otherwise = error "Unsupported Packet version or type in fingerprint"
20 where
21 material = LZ.concat $ OpenPGP.fingerprint_material p
22
23 hexify = map toUpper . hexString . BS.unpack
24
25 hexString :: [Word8] -> String
26 hexString = foldr (pad `oo` showHex) ""
27 where
28 pad s | odd $ length s = '0':s
29 | otherwise = s
30
31 oo :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c
32 oo = (.) . (.)
33