diff options
author | Stephen Paul Weber <singpolyma@singpolyma.net> | 2012-04-25 15:05:59 -0500 |
---|---|---|
committer | Stephen Paul Weber <singpolyma@singpolyma.net> | 2012-04-25 15:05:59 -0500 |
commit | 945512be4374fd48bfe09ca4018fc5fe94d3f26e (patch) | |
tree | 7aa7ee9efa7edac3e220757cce0dbea9e803271e | |
parent | 4e01c764e40a1c7ba45ed1e0d1a44677338ac549 (diff) |
Remove BaseConvert
We only use it for hex, which is in base, and base even has a utility
for arbitrary-base conversions.
-rw-r--r-- | Data/BaseConvert.hs | 30 | ||||
-rw-r--r-- | Data/OpenPGP.hs | 12 | ||||
-rw-r--r-- | Data/OpenPGP/Crypto.hs | 7 | ||||
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | openpgp.cabal | 3 |
5 files changed, 12 insertions, 44 deletions
diff --git a/Data/BaseConvert.hs b/Data/BaseConvert.hs deleted file mode 100644 index 655f593..0000000 --- a/Data/BaseConvert.hs +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | module Data.BaseConvert (toString, toNum, toAlphaDigit, fromAlphaDigit) where | ||
2 | |||
3 | import Data.Sequence | ||
4 | import Data.Foldable (toList) | ||
5 | import Data.List | ||
6 | import Data.Char | ||
7 | |||
8 | digit_alphabet :: [Char] | ||
9 | digit_alphabet = ['0'..'9'] ++ ['A'..] | ||
10 | |||
11 | toBase :: (Integral a) => a -> a -> [a] | ||
12 | toBase _ 0 = [0] | ||
13 | toBase b v = toList $ | ||
14 | unfoldl (\n -> if n == 0 then Nothing else Just (n `divMod` b)) v | ||
15 | |||
16 | toAlphaDigit :: (Integral a) => a -> Char | ||
17 | toAlphaDigit = (digit_alphabet !!) . fromIntegral | ||
18 | |||
19 | toString :: (Integral a) => a -> a -> String | ||
20 | toString b v = map toAlphaDigit (toBase b v) | ||
21 | |||
22 | fromAlphaDigit :: (Num a) => Char -> a | ||
23 | fromAlphaDigit v = fromIntegral n | ||
24 | where Just n = elemIndex (toUpper v) digit_alphabet | ||
25 | |||
26 | fromBase :: (Num a) => a -> [a] -> a | ||
27 | fromBase b = foldl (\n k -> n * b + k) 0 | ||
28 | |||
29 | toNum :: (Num a) => a -> String -> a | ||
30 | toNum b v = fromBase b (map fromAlphaDigit v) | ||
diff --git a/Data/OpenPGP.hs b/Data/OpenPGP.hs index 54fc132..a5c2bef 100644 --- a/Data/OpenPGP.hs +++ b/Data/OpenPGP.hs | |||
@@ -45,9 +45,11 @@ module Data.OpenPGP ( | |||
45 | decode_s2k_count, encode_s2k_count | 45 | decode_s2k_count, encode_s2k_count |
46 | ) where | 46 | ) where |
47 | 47 | ||
48 | import Numeric | ||
48 | import Control.Monad | 49 | import Control.Monad |
49 | import Data.Bits | 50 | import Data.Bits |
50 | import Data.Word | 51 | import Data.Word |
52 | import Data.Char | ||
51 | import Data.Maybe | 53 | import Data.Maybe |
52 | import Data.Map (Map, (!)) | 54 | import Data.Map (Map, (!)) |
53 | import qualified Data.Map as Map | 55 | import qualified Data.Map as Map |
@@ -61,8 +63,6 @@ import qualified Codec.Compression.Zlib.Raw as Zip | |||
61 | import qualified Codec.Compression.Zlib as Zlib | 63 | import qualified Codec.Compression.Zlib as Zlib |
62 | import qualified Codec.Compression.BZip as BZip2 | 64 | import qualified Codec.Compression.BZip as BZip2 |
63 | 65 | ||
64 | import qualified Data.BaseConvert as BaseConvert | ||
65 | |||
66 | data Packet = | 66 | data Packet = |
67 | SignaturePacket { | 67 | SignaturePacket { |
68 | version::Word8, | 68 | version::Word8, |
@@ -241,7 +241,7 @@ put_packet (OnePassSignaturePacket { version = version, | |||
241 | nested = nested }) = | 241 | nested = nested }) = |
242 | (LZ.concat [ encode version, encode signature_type, | 242 | (LZ.concat [ encode version, encode signature_type, |
243 | encode hash_algorithm, encode key_algorithm, | 243 | encode hash_algorithm, encode key_algorithm, |
244 | encode (BaseConvert.toNum 16 key_id :: Word64), | 244 | encode (fst $ head $ readHex key_id :: Word64), |
245 | encode nested ], 4) | 245 | encode nested ], 4) |
246 | put_packet (SecretKeyPacket { version = version, timestamp = timestamp, | 246 | put_packet (SecretKeyPacket { version = version, timestamp = timestamp, |
247 | key_algorithm = algorithm, key = key, | 247 | key_algorithm = algorithm, key = key, |
@@ -344,7 +344,7 @@ parse_packet 4 = do | |||
344 | signature_type = signature_type, | 344 | signature_type = signature_type, |
345 | hash_algorithm = hash_algo, | 345 | hash_algorithm = hash_algo, |
346 | key_algorithm = key_algo, | 346 | key_algorithm = key_algo, |
347 | key_id = BaseConvert.toString 16 key_id, | 347 | key_id = map toUpper $ showHex key_id "", |
348 | nested = nested | 348 | nested = nested |
349 | } | 349 | } |
350 | -- SecretKeyPacket, http://tools.ietf.org/html/rfc4880#section-5.5.3 | 350 | -- SecretKeyPacket, http://tools.ietf.org/html/rfc4880#section-5.5.3 |
@@ -628,7 +628,7 @@ put_signature_subpacket :: SignatureSubpacket -> (LZ.ByteString, Word8) | |||
628 | put_signature_subpacket (SignatureCreationTimePacket time) = | 628 | put_signature_subpacket (SignatureCreationTimePacket time) = |
629 | (encode time, 2) | 629 | (encode time, 2) |
630 | put_signature_subpacket (IssuerPacket keyid) = | 630 | put_signature_subpacket (IssuerPacket keyid) = |
631 | (encode (BaseConvert.toNum 16 keyid :: Word64), 16) | 631 | (encode (fst $ head $ readHex keyid :: Word64), 16) |
632 | put_signature_subpacket (UnsupportedSignatureSubpacket tag bytes) = | 632 | put_signature_subpacket (UnsupportedSignatureSubpacket tag bytes) = |
633 | (bytes, tag) | 633 | (bytes, tag) |
634 | 634 | ||
@@ -647,7 +647,7 @@ parse_signature_subpacket 2 = fmap SignatureCreationTimePacket get | |||
647 | -- IssuerPacket, http://tools.ietf.org/html/rfc4880#section-5.2.3.5 | 647 | -- IssuerPacket, http://tools.ietf.org/html/rfc4880#section-5.2.3.5 |
648 | parse_signature_subpacket 16 = do | 648 | parse_signature_subpacket 16 = do |
649 | keyid <- get :: Get Word64 | 649 | keyid <- get :: Get Word64 |
650 | return $ IssuerPacket (BaseConvert.toString 16 keyid) | 650 | return $ IssuerPacket (map toUpper $ showHex keyid "") |
651 | -- Represent unsupported packets as their tag and literal bytes | 651 | -- Represent unsupported packets as their tag and literal bytes |
652 | parse_signature_subpacket tag = | 652 | parse_signature_subpacket tag = |
653 | fmap (UnsupportedSignatureSubpacket tag) getRemainingLazyByteString | 653 | fmap (UnsupportedSignatureSubpacket tag) getRemainingLazyByteString |
diff --git a/Data/OpenPGP/Crypto.hs b/Data/OpenPGP/Crypto.hs index 173fe08..052489d 100644 --- a/Data/OpenPGP/Crypto.hs +++ b/Data/OpenPGP/Crypto.hs | |||
@@ -6,7 +6,9 @@ | |||
6 | -- > import qualified Data.OpenPGP.Crypto as OpenPGP | 6 | -- > import qualified Data.OpenPGP.Crypto as OpenPGP |
7 | module Data.OpenPGP.Crypto (sign, verify, fingerprint) where | 7 | module Data.OpenPGP.Crypto (sign, verify, fingerprint) where |
8 | 8 | ||
9 | import Numeric | ||
9 | import Data.Word | 10 | import Data.Word |
11 | import Data.Char | ||
10 | import Data.List (find) | 12 | import Data.List (find) |
11 | import Data.Map ((!)) | 13 | import Data.Map ((!)) |
12 | import qualified Data.ByteString.Lazy as LZ | 14 | import qualified Data.ByteString.Lazy as LZ |
@@ -22,16 +24,15 @@ import qualified Data.Digest.SHA384 as SHA384 | |||
22 | import qualified Data.Digest.SHA512 as SHA512 | 24 | import qualified Data.Digest.SHA512 as SHA512 |
23 | 25 | ||
24 | import qualified Data.OpenPGP as OpenPGP | 26 | import qualified Data.OpenPGP as OpenPGP |
25 | import qualified Data.BaseConvert as BaseConvert | ||
26 | 27 | ||
27 | -- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket | 28 | -- | Generate a key fingerprint from a PublicKeyPacket or SecretKeyPacket |
28 | -- <http://tools.ietf.org/html/rfc4880#section-12.2> | 29 | -- <http://tools.ietf.org/html/rfc4880#section-12.2> |
29 | fingerprint :: OpenPGP.Packet -> String | 30 | fingerprint :: OpenPGP.Packet -> String |
30 | fingerprint p | OpenPGP.version p == 4 = | 31 | fingerprint p | OpenPGP.version p == 4 = |
31 | BaseConvert.toString 16 $ SHA1.toInteger $ SHA1.hash $ | 32 | map toUpper $ (`showHex` "") $ SHA1.toInteger $ SHA1.hash $ |
32 | LZ.unpack (LZ.concat (OpenPGP.fingerprint_material p)) | 33 | LZ.unpack (LZ.concat (OpenPGP.fingerprint_material p)) |
33 | fingerprint p | OpenPGP.version p `elem` [2, 3] = | 34 | fingerprint p | OpenPGP.version p `elem` [2, 3] = |
34 | concatMap (BaseConvert.toString 16) $ | 35 | map toUpper $ foldr showHex "" $ |
35 | MD5.hash $ LZ.unpack (LZ.concat (OpenPGP.fingerprint_material p)) | 36 | MD5.hash $ LZ.unpack (LZ.concat (OpenPGP.fingerprint_material p)) |
36 | fingerprint _ = error "Unsupported Packet version or type in fingerprint." | 37 | fingerprint _ = error "Unsupported Packet version or type in fingerprint." |
37 | 38 | ||
@@ -51,9 +51,9 @@ clean: | |||
51 | debian/control: openpgp.cabal | 51 | debian/control: openpgp.cabal |
52 | cabal-debian --update-debianization | 52 | cabal-debian --update-debianization |
53 | 53 | ||
54 | dist/build/libHSopenpgp-$(VERSION).a: openpgp.cabal dist/setup-config Data/BaseConvert.hs Data/OpenPGP.hs Data/OpenPGP/Crypto.hs | 54 | dist/build/libHSopenpgp-$(VERSION).a: openpgp.cabal dist/setup-config Data/OpenPGP.hs Data/OpenPGP/Crypto.hs |
55 | cabal build --ghc-options="$(GHCFLAGS)" | 55 | cabal build --ghc-options="$(GHCFLAGS)" |
56 | 56 | ||
57 | dist/openpgp-$(VERSION).tar.gz: openpgp.cabal dist/setup-config Data/BaseConvert.hs Data/OpenPGP.hs Data/OpenPGP/Crypto.hs README | 57 | dist/openpgp-$(VERSION).tar.gz: openpgp.cabal dist/setup-config Data/OpenPGP.hs Data/OpenPGP/Crypto.hs README |
58 | cabal check | 58 | cabal check |
59 | cabal sdist | 59 | cabal sdist |
diff --git a/openpgp.cabal b/openpgp.cabal index c20f6fc..0a40b94 100644 --- a/openpgp.cabal +++ b/openpgp.cabal | |||
@@ -128,9 +128,6 @@ library | |||
128 | Data.OpenPGP | 128 | Data.OpenPGP |
129 | Data.OpenPGP.Crypto | 129 | Data.OpenPGP.Crypto |
130 | 130 | ||
131 | other-modules: | ||
132 | Data.BaseConvert | ||
133 | |||
134 | build-depends: | 131 | build-depends: |
135 | base == 4.*, | 132 | base == 4.*, |
136 | containers, | 133 | containers, |