summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Paul Weber <singpolyma@singpolyma.net>2012-04-25 15:05:59 -0500
committerStephen Paul Weber <singpolyma@singpolyma.net>2012-04-25 15:05:59 -0500
commit945512be4374fd48bfe09ca4018fc5fe94d3f26e (patch)
tree7aa7ee9efa7edac3e220757cce0dbea9e803271e
parent4e01c764e40a1c7ba45ed1e0d1a44677338ac549 (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.hs30
-rw-r--r--Data/OpenPGP.hs12
-rw-r--r--Data/OpenPGP/Crypto.hs7
-rw-r--r--Makefile4
-rw-r--r--openpgp.cabal3
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 @@
1module Data.BaseConvert (toString, toNum, toAlphaDigit, fromAlphaDigit) where
2
3import Data.Sequence
4import Data.Foldable (toList)
5import Data.List
6import Data.Char
7
8digit_alphabet :: [Char]
9digit_alphabet = ['0'..'9'] ++ ['A'..]
10
11toBase :: (Integral a) => a -> a -> [a]
12toBase _ 0 = [0]
13toBase b v = toList $
14 unfoldl (\n -> if n == 0 then Nothing else Just (n `divMod` b)) v
15
16toAlphaDigit :: (Integral a) => a -> Char
17toAlphaDigit = (digit_alphabet !!) . fromIntegral
18
19toString :: (Integral a) => a -> a -> String
20toString b v = map toAlphaDigit (toBase b v)
21
22fromAlphaDigit :: (Num a) => Char -> a
23fromAlphaDigit v = fromIntegral n
24 where Just n = elemIndex (toUpper v) digit_alphabet
25
26fromBase :: (Num a) => a -> [a] -> a
27fromBase b = foldl (\n k -> n * b + k) 0
28
29toNum :: (Num a) => a -> String -> a
30toNum 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
48import Numeric
48import Control.Monad 49import Control.Monad
49import Data.Bits 50import Data.Bits
50import Data.Word 51import Data.Word
52import Data.Char
51import Data.Maybe 53import Data.Maybe
52import Data.Map (Map, (!)) 54import Data.Map (Map, (!))
53import qualified Data.Map as Map 55import qualified Data.Map as Map
@@ -61,8 +63,6 @@ import qualified Codec.Compression.Zlib.Raw as Zip
61import qualified Codec.Compression.Zlib as Zlib 63import qualified Codec.Compression.Zlib as Zlib
62import qualified Codec.Compression.BZip as BZip2 64import qualified Codec.Compression.BZip as BZip2
63 65
64import qualified Data.BaseConvert as BaseConvert
65
66data Packet = 66data 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)
246put_packet (SecretKeyPacket { version = version, timestamp = timestamp, 246put_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)
628put_signature_subpacket (SignatureCreationTimePacket time) = 628put_signature_subpacket (SignatureCreationTimePacket time) =
629 (encode time, 2) 629 (encode time, 2)
630put_signature_subpacket (IssuerPacket keyid) = 630put_signature_subpacket (IssuerPacket keyid) =
631 (encode (BaseConvert.toNum 16 keyid :: Word64), 16) 631 (encode (fst $ head $ readHex keyid :: Word64), 16)
632put_signature_subpacket (UnsupportedSignatureSubpacket tag bytes) = 632put_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
648parse_signature_subpacket 16 = do 648parse_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
652parse_signature_subpacket tag = 652parse_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
7module Data.OpenPGP.Crypto (sign, verify, fingerprint) where 7module Data.OpenPGP.Crypto (sign, verify, fingerprint) where
8 8
9import Numeric
9import Data.Word 10import Data.Word
11import Data.Char
10import Data.List (find) 12import Data.List (find)
11import Data.Map ((!)) 13import Data.Map ((!))
12import qualified Data.ByteString.Lazy as LZ 14import qualified Data.ByteString.Lazy as LZ
@@ -22,16 +24,15 @@ import qualified Data.Digest.SHA384 as SHA384
22import qualified Data.Digest.SHA512 as SHA512 24import qualified Data.Digest.SHA512 as SHA512
23 25
24import qualified Data.OpenPGP as OpenPGP 26import qualified Data.OpenPGP as OpenPGP
25import 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>
29fingerprint :: OpenPGP.Packet -> String 30fingerprint :: OpenPGP.Packet -> String
30fingerprint p | OpenPGP.version p == 4 = 31fingerprint 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))
33fingerprint p | OpenPGP.version p `elem` [2, 3] = 34fingerprint 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))
36fingerprint _ = error "Unsupported Packet version or type in fingerprint." 37fingerprint _ = error "Unsupported Packet version or type in fingerprint."
37 38
diff --git a/Makefile b/Makefile
index 5aa7ef6..10f66fa 100644
--- a/Makefile
+++ b/Makefile
@@ -51,9 +51,9 @@ clean:
51debian/control: openpgp.cabal 51debian/control: openpgp.cabal
52 cabal-debian --update-debianization 52 cabal-debian --update-debianization
53 53
54dist/build/libHSopenpgp-$(VERSION).a: openpgp.cabal dist/setup-config Data/BaseConvert.hs Data/OpenPGP.hs Data/OpenPGP/Crypto.hs 54dist/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
57dist/openpgp-$(VERSION).tar.gz: openpgp.cabal dist/setup-config Data/BaseConvert.hs Data/OpenPGP.hs Data/OpenPGP/Crypto.hs README 57dist/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,