diff options
Diffstat (limited to 'Data/OpenPGP/Util/Fingerprint.hs')
-rw-r--r-- | Data/OpenPGP/Util/Fingerprint.hs | 37 |
1 files changed, 21 insertions, 16 deletions
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 @@ | |||
1 | {-# LANGUAGE CPP #-} | 1 | {-# LANGUAGE CPP #-} |
2 | module Data.OpenPGP.Util.Fingerprint (fingerprint) where | 2 | module Data.OpenPGP.Util.Fingerprint (fingerprint,Fingerprint(..)) where |
3 | 3 | ||
4 | import qualified Data.OpenPGP as OpenPGP | 4 | import qualified Data.OpenPGP as OpenPGP |
5 | import qualified Data.ByteString as BS | 5 | import qualified Data.ByteString as BS |
@@ -17,12 +17,29 @@ import Crypto.Hash.MD5 as MD5 | |||
17 | import Crypto.Hash.SHA1 as SHA1 | 17 | import Crypto.Hash.SHA1 as SHA1 |
18 | #endif | 18 | #endif |
19 | 19 | ||
20 | oo :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c | ||
21 | oo = (.) . (.) | ||
22 | |||
23 | newtype Fingerprint = Fingerprint BS.ByteString | ||
24 | |||
25 | instance Show Fingerprint where | ||
26 | show (Fingerprint bs) = hexify bs | ||
27 | where | ||
28 | hexify = map toUpper . hexString . BS.unpack | ||
29 | |||
30 | hexString :: [Word8] -> String | ||
31 | hexString = foldr (pad `oo` showHex) "" | ||
32 | where | ||
33 | pad s | odd $ length s = '0':s | ||
34 | | otherwise = s | ||
35 | |||
36 | |||
20 | -- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket | 37 | -- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket |
21 | -- <http://tools.ietf.org/html/rfc4880#section-12.2> | 38 | -- <http://tools.ietf.org/html/rfc4880#section-12.2> |
22 | fingerprint :: OpenPGP.Packet -> String | 39 | fingerprint :: OpenPGP.Packet -> Fingerprint |
23 | fingerprint p | 40 | fingerprint p |
24 | | OpenPGP.version p == 4 = hexify $ sha1 material | 41 | | OpenPGP.version p == 4 = Fingerprint $ sha1 material |
25 | | OpenPGP.version p `elem` [2, 3] = hexify $ md5 material | 42 | | OpenPGP.version p `elem` [2, 3] = Fingerprint $ md5 material |
26 | | otherwise = error "Unsupported Packet version or type in fingerprint" | 43 | | otherwise = error "Unsupported Packet version or type in fingerprint" |
27 | where | 44 | where |
28 | 45 | ||
@@ -35,15 +52,3 @@ fingerprint p | |||
35 | #endif | 52 | #endif |
36 | 53 | ||
37 | material = LZ.concat $ OpenPGP.fingerprint_material p | 54 | material = LZ.concat $ OpenPGP.fingerprint_material p |
38 | |||
39 | hexify = map toUpper . hexString . BS.unpack | ||
40 | |||
41 | hexString :: [Word8] -> String | ||
42 | hexString = foldr (pad `oo` showHex) "" | ||
43 | where | ||
44 | pad s | odd $ length s = '0':s | ||
45 | | otherwise = s | ||
46 | |||
47 | oo :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c | ||
48 | oo = (.) . (.) | ||
49 | |||