summaryrefslogtreecommitdiff
path: root/Data/OpenPGP/Util/Base.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Data/OpenPGP/Util/Base.hs')
-rw-r--r--Data/OpenPGP/Util/Base.hs67
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 @@
1module Data.OpenPGP.Util.Base where
2
3import qualified Data.ByteString as BS
4import qualified Data.ByteString.Lazy as LZ
5import Data.Binary (encode)
6
7import Data.OpenPGP as OpenPGP
8import Crypto.Hash.MD5 as MD5
9import Crypto.Hash.SHA1 as SHA1
10import Crypto.Hash.SHA256 as SHA256
11import Crypto.Hash.SHA384 as SHA384
12import Crypto.Hash.SHA512 as SHA512
13import Crypto.Hash.SHA224 as SHA224
14import Crypto.Hash.RIPEMD160 as RIPEMD160
15import qualified Crypto.PubKey.RSA as Vincent.RSA
16import Crypto.PubKey.HashDescr as Vincent
17
18import Data.OpenPGP.Util.Fingerprint (fingerprint)
19
20hashBySymbol OpenPGP.MD5 = MD5.hashlazy
21hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy
22hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy
23hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy
24hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy
25hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy
26hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy
27
28toStrictBS :: LZ.ByteString -> BS.ByteString
29toStrictBS = BS.concat . LZ.toChunks
30
31toLazyBS :: BS.ByteString -> LZ.ByteString
32toLazyBS = LZ.fromChunks . (:[])
33
34find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet
35find_key = OpenPGP.find_key fingerprint
36
37
38
39keyParam :: Char -> OpenPGP.Packet -> Integer
40keyParam 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
46integerBytesize :: Integer -> Int
47integerBytesize i = fromIntegral $ LZ.length (encode (OpenPGP.MPI i)) - 2
48
49rsaKey :: OpenPGP.Packet -> Vincent.RSA.PublicKey
50rsaKey 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
57hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5
58hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1
59hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160
60hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256
61hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384
62hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512
63hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224
64hashAlgoDesc _ =
65 error "Unsupported HashAlgorithm in hashAlgoDesc"
66
67