diff options
Diffstat (limited to 'Data/OpenPGP/Util/Base.hs')
-rw-r--r-- | Data/OpenPGP/Util/Base.hs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Data/OpenPGP/Util/Base.hs b/Data/OpenPGP/Util/Base.hs new file mode 100644 index 0000000..802d503 --- /dev/null +++ b/Data/OpenPGP/Util/Base.hs | |||
@@ -0,0 +1,67 @@ | |||
1 | module Data.OpenPGP.Util.Base where | ||
2 | |||
3 | import qualified Data.ByteString as BS | ||
4 | import qualified Data.ByteString.Lazy as LZ | ||
5 | import Data.Binary (encode) | ||
6 | |||
7 | import Data.OpenPGP as OpenPGP | ||
8 | import Crypto.Hash.MD5 as MD5 | ||
9 | import Crypto.Hash.SHA1 as SHA1 | ||
10 | import Crypto.Hash.SHA256 as SHA256 | ||
11 | import Crypto.Hash.SHA384 as SHA384 | ||
12 | import Crypto.Hash.SHA512 as SHA512 | ||
13 | import Crypto.Hash.SHA224 as SHA224 | ||
14 | import Crypto.Hash.RIPEMD160 as RIPEMD160 | ||
15 | import qualified Crypto.PubKey.RSA as Vincent.RSA | ||
16 | import Crypto.PubKey.HashDescr as Vincent | ||
17 | |||
18 | import Data.OpenPGP.Util.Fingerprint (fingerprint) | ||
19 | |||
20 | hashBySymbol OpenPGP.MD5 = MD5.hashlazy | ||
21 | hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy | ||
22 | hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy | ||
23 | hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy | ||
24 | hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy | ||
25 | hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy | ||
26 | hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy | ||
27 | |||
28 | toStrictBS :: LZ.ByteString -> BS.ByteString | ||
29 | toStrictBS = BS.concat . LZ.toChunks | ||
30 | |||
31 | toLazyBS :: BS.ByteString -> LZ.ByteString | ||
32 | toLazyBS = LZ.fromChunks . (:[]) | ||
33 | |||
34 | find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet | ||
35 | find_key = OpenPGP.find_key fingerprint | ||
36 | |||
37 | |||
38 | |||
39 | keyParam :: Char -> OpenPGP.Packet -> Integer | ||
40 | keyParam c k = fromJustMPI $ lookup c (OpenPGP.key k) | ||
41 | where | ||
42 | fromJustMPI :: Maybe OpenPGP.MPI -> Integer | ||
43 | fromJustMPI (Just (OpenPGP.MPI x)) = x | ||
44 | fromJustMPI _ = error "Not a Just MPI, Data.OpenPGP.CryptoAPI" | ||
45 | |||
46 | integerBytesize :: Integer -> Int | ||
47 | integerBytesize i = fromIntegral $ LZ.length (encode (OpenPGP.MPI i)) - 2 | ||
48 | |||
49 | rsaKey :: OpenPGP.Packet -> Vincent.RSA.PublicKey | ||
50 | rsaKey k = | ||
51 | Vincent.RSA.PublicKey (integerBytesize n) n (keyParam 'e' k) | ||
52 | where | ||
53 | n = keyParam 'n' k | ||
54 | |||
55 | -- http://tools.ietf.org/html/rfc3447#page-43 | ||
56 | -- http://tools.ietf.org/html/rfc4880#section-5.2.2 | ||
57 | hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 | ||
58 | hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 | ||
59 | hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 | ||
60 | hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 | ||
61 | hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 | ||
62 | hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 | ||
63 | hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 | ||
64 | hashAlgoDesc _ = | ||
65 | error "Unsupported HashAlgorithm in hashAlgoDesc" | ||
66 | |||
67 | |||