diff options
author | joe <joe@jerkface.net> | 2013-12-14 22:34:26 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-12-14 22:34:26 -0500 |
commit | ce0d32ef83ccf15198bdd5248faa02abbcf2f769 (patch) | |
tree | 96155973d7af58bbb79a8f67a21f646af46cb47f /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.hs | 33 |
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 @@ | |||
1 | module Data.OpenPGP.Util.Fingerprint (fingerprint) where | ||
2 | |||
3 | import qualified Data.OpenPGP as OpenPGP | ||
4 | import qualified Data.ByteString as BS | ||
5 | import qualified Data.ByteString.Lazy as LZ | ||
6 | import Data.Char (toUpper) | ||
7 | import Data.Word (Word8) | ||
8 | import Numeric (showHex) | ||
9 | |||
10 | import Crypto.Hash.MD5 as MD5 | ||
11 | import 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> | ||
15 | fingerprint :: OpenPGP.Packet -> String | ||
16 | fingerprint 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 | |||