Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 49cf7d1

Browse files
committed
reformatting, mostly hindent
1 parent 55f8209 commit 49cf7d1

File tree

5 files changed

+107
-133
lines changed

5 files changed

+107
-133
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,3 @@ Small note: If editing the README, please conform to the [standard-readme](https
3535
## License
3636

3737
[Copyright](LICENSE) © 2015, Luke Hoersten
38-

Setup.hs

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import Distribution.Simple
2+
23
main = defaultMain

main/Main.hs

+59-74
Original file line numberDiff line numberDiff line change
@@ -2,109 +2,94 @@
22

33
module Main where
44

5-
import Crypto.Hash (Digest)
6-
import qualified Crypto.Hash as CH
7-
import Data.Byteable (toBytes)
8-
import Data.ByteString (ByteString)
9-
import Data.ByteString.Lazy (toStrict)
10-
import Data.Monoid ((<>))
11-
import Options.Applicative
12-
13-
import System.IO.Streams (InputStream, stdin, stdout,
14-
withFileAsInput, write)
15-
import System.IO.Streams.Crypto (hashInputStream)
16-
17-
import qualified Data.Multihash.Base as MB
18-
import qualified Data.Multihash.Digest as MH
19-
20-
21-
data Termination = Null | Newline deriving (Show, Eq)
22-
data Config =
23-
Config
24-
{ cfFile :: Maybe FilePath
25-
, cfAlgo :: MH.HashAlgorithm
26-
, cfBase :: MB.BaseEncoding
27-
, cfHash :: Maybe MH.Digest
28-
, cfTerm :: Termination
29-
} deriving Show
30-
5+
import Crypto.Hash (Digest)
6+
import qualified Crypto.Hash as CH
7+
import Data.ByteString (ByteString)
8+
import Data.ByteString.Lazy (toStrict)
9+
import Data.Byteable (toBytes)
10+
import Data.Monoid ((<>))
11+
import Options.Applicative
12+
13+
import System.IO.Streams (InputStream, stdin, stdout, withFileAsInput, write)
14+
import System.IO.Streams.Crypto (hashInputStream)
15+
16+
import qualified Data.Multihash.Base as MB
17+
import qualified Data.Multihash.Digest as MH
18+
19+
data Termination
20+
= Null
21+
| Newline
22+
deriving (Show, Eq)
23+
24+
data Config = Config
25+
{ cfFile :: Maybe FilePath
26+
, cfAlgo :: MH.HashAlgorithm
27+
, cfBase :: MB.BaseEncoding
28+
, cfHash :: Maybe MH.Digest
29+
, cfTerm :: Termination
30+
} deriving (Show)
3131

3232
main :: IO ()
33-
main = do
33+
main
3434
-- TODO add file checking
35-
config <- execParser opts
36-
digest <- maybe (hashStdin config) (hashFile config) $ cfFile config
37-
write (multihash config digest) stdout
35+
= do
36+
config <- execParser opts
37+
digest <- maybe (hashStdin config) (hashFile config) $ cfFile config
38+
write (multihash config digest) stdout
3839
where
3940
hashStdin config = hash (cfAlgo config) stdin
4041
hashFile config file = withFileAsInput file . hash $ cfAlgo config
4142
multihash (Config _file algo base _hash term) =
42-
Just . toStrict . line term . MB.encode base . MH.encode algo
43-
44-
line Null = (<> "\0")
43+
Just . toStrict . line term . MB.encode base . MH.encode algo
44+
line Null = (<> "\0")
4545
line Newline = (<> "\n")
4646

47-
4847
-- TODO add BLAKE support
4948
hash :: MH.HashAlgorithm -> InputStream ByteString -> IO MH.Digest
50-
hash MH.SHA1 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA1))
49+
hash MH.SHA1 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA1))
5150
hash MH.SHA256 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA256))
5251
hash MH.SHA512 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA512))
53-
hash MH.SHA3 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA3_256))
52+
hash MH.SHA3 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA3_256))
5453
hash MH.BLAKE2B _ = undefined
5554
hash MH.BLAKE2S _ = undefined
5655

57-
5856
opts :: ParserInfo Config
59-
opts = info
60-
(helper <*> (Config
61-
<$> fileArg
62-
<*> algoOpt
63-
<*> baseOpt
64-
<*> checkOpt
65-
<*> nullTermFlag
66-
))
67-
(fullDesc
68-
<> header "Generate a multihash for the given input."
69-
<> progDesc "Hash from FILE or stdin if not given.")
70-
57+
opts =
58+
info
59+
(helper <*>
60+
(Config <$> fileArg <*> algoOpt <*> baseOpt <*> checkOpt <*> nullTermFlag))
61+
(fullDesc <> header "Generate a multihash for the given input." <>
62+
progDesc "Hash from FILE or stdin if not given.")
7163

7264
algoOpt :: Parser MH.HashAlgorithm
7365
algoOpt =
74-
option auto
75-
$ long "algorithm"
76-
<> short 'a'
77-
<> metavar "ALGO"
78-
<> showDefault <> value MH.SHA256
79-
<> help ("Hash algorithm to apply to input, ignored if checking hash " <> show ([minBound..] :: [MH.HashAlgorithm]))
80-
66+
option auto $
67+
long "algorithm" <> short 'a' <> metavar "ALGO" <> showDefault <>
68+
value MH.SHA256 <>
69+
help
70+
("Hash algorithm to apply to input, ignored if checking hash " <>
71+
show ([minBound ..] :: [MH.HashAlgorithm]))
8172

8273
baseOpt :: Parser MB.BaseEncoding
8374
baseOpt =
84-
option auto
85-
$ long "encoding"
86-
<> short 'e'
87-
<> metavar "ENCODING"
88-
<> showDefault <> value MB.Base58
89-
<> help ("Base encoding of output digest, ignored if checking hash " <> show ([minBound..] :: [MB.BaseEncoding]))
90-
75+
option auto $
76+
long "encoding" <> short 'e' <> metavar "ENCODING" <> showDefault <>
77+
value MB.Base58 <>
78+
help
79+
("Base encoding of output digest, ignored if checking hash " <>
80+
show ([minBound ..] :: [MB.BaseEncoding]))
9181

9282
checkOpt :: Parser (Maybe MH.Digest)
9383
checkOpt =
94-
optional . option auto
95-
$ long "check"
96-
<> short 'c'
97-
<> metavar "DIGEST"
98-
<> help "Check for matching digest"
99-
84+
optional . option auto $
85+
long "check" <> short 'c' <> metavar "DIGEST" <>
86+
help "Check for matching digest"
10087

10188
nullTermFlag :: Parser Termination
10289
nullTermFlag =
103-
flag Newline Null
104-
$ long "print0"
105-
<> short '0'
106-
<> help "End filenames with NUL, for use with xargs"
107-
90+
flag Newline Null $
91+
long "print0" <> short '0' <>
92+
help "End filenames with NUL, for use with xargs"
10893

10994
fileArg :: Parser (Maybe FilePath)
11095
fileArg = optional . argument str $ metavar "FILE"

src/Data/Multihash/Base.hs

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
module Data.Multihash.Base where
22

33
-- import qualified Data.ByteString.Base32 as B32
4-
import qualified Data.ByteString.Base58 as B58
4+
import qualified Data.ByteString.Base58 as B58
55
import qualified Data.ByteString.Base64.Lazy as B64
6-
import Data.ByteString.Lazy (ByteString, fromStrict, toStrict)
7-
import Data.Either (Either)
8-
import qualified Data.Hex as B16
9-
6+
import Data.ByteString.Lazy (ByteString, fromStrict, toStrict)
7+
import Data.Either (Either)
8+
import qualified Data.Hex as B16
109

1110
-- TODO add Base32 encoding
1211
data BaseEncoding
13-
= Base2 -- ^ Raw binary encoding
14-
| Base16 -- ^ Hexadecimal encoding
15-
| Base58 -- ^ Bitcoin encoding
16-
| Base64
17-
deriving (Show, Read, Eq, Enum, Bounded)
18-
12+
= Base2 -- ^ Raw binary encoding
13+
| Base16 -- ^ Hexadecimal encoding
14+
| Base58 -- ^ Bitcoin encoding
15+
| Base64
16+
deriving (Show, Read, Eq, Enum, Bounded)
1917

2018
encode :: BaseEncoding -> ByteString -> ByteString
21-
encode Base2 = id
19+
encode Base2 = id
2220
encode Base16 = B16.hex
2321
-- encode Base32 = B32.encode
2422
encode Base58 = fromStrict . B58.encodeBase58 B58.bitcoinAlphabet . toStrict
2523
encode Base64 = B64.encode
2624

27-
2825
decode :: BaseEncoding -> ByteString -> Either String ByteString
29-
decode Base2 = return . id
26+
decode Base2 = return . id
3027
decode Base16 = B16.unhex
3128
-- decode Base32 = B32.decode
32-
decode Base58 = maybe (Left "Failed to parse") (Right . fromStrict) . B58.decodeBase58 B58.bitcoinAlphabet . toStrict
29+
decode Base58 =
30+
maybe (Left "Failed to parse") (Right . fromStrict) .
31+
B58.decodeBase58 B58.bitcoinAlphabet . toStrict
3332
decode Base64 = B64.decode

src/Data/Multihash/Digest.hs

+33-43
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
module Data.Multihash.Digest where
22

3-
4-
import Control.Applicative ((<$>))
5-
import Data.Attoparsec.ByteString (Parser, parseOnly)
3+
import Control.Applicative ((<$>))
4+
import Data.Attoparsec.ByteString (Parser, parseOnly)
65
import qualified Data.Attoparsec.ByteString as A
7-
import qualified Data.ByteString as BS
8-
import Data.ByteString.Builder (Builder, byteString,
9-
toLazyByteString)
10-
import qualified Data.ByteString.Builder as BB
11-
import qualified Data.ByteString.Lazy as BL
12-
import Data.Monoid ((<>))
13-
14-
data MultihashDigest =
15-
MultihashDigest
16-
{ algorithm :: !HashAlgorithm
17-
, length :: !Length
18-
, digest :: !Digest
19-
} deriving (Show, Eq)
20-
6+
import qualified Data.ByteString as BS
7+
import Data.ByteString.Builder (Builder, byteString, toLazyByteString)
8+
import qualified Data.ByteString.Builder as BB
9+
import qualified Data.ByteString.Lazy as BL
10+
import Data.Monoid ((<>))
11+
12+
data MultihashDigest = MultihashDigest
13+
{ algorithm :: !HashAlgorithm
14+
, length :: !Length
15+
, digest :: !Digest
16+
} deriving (Show, Eq)
2117

2218
type Length = Int
23-
type Digest = BS.ByteString
2419

20+
type Digest = BS.ByteString
2521

2622
data HashAlgorithm
27-
= SHA1
28-
| SHA256
29-
| SHA512
30-
| SHA3
31-
| BLAKE2B
32-
| BLAKE2S
33-
deriving (Show, Read, Eq, Enum, Bounded)
34-
23+
= SHA1
24+
| SHA256
25+
| SHA512
26+
| SHA3
27+
| BLAKE2B
28+
| BLAKE2S
29+
deriving (Show, Read, Eq, Enum, Bounded)
3530

3631
fromCode :: Int -> HashAlgorithm
3732
fromCode 0x11 = SHA1
@@ -42,34 +37,29 @@ fromCode 0x40 = BLAKE2B
4237
fromCode 0x41 = BLAKE2S
4338
fromCode _ = error "Unknown hash function code"
4439

45-
4640
toCode :: HashAlgorithm -> Int
47-
toCode SHA1 = 0x11
48-
toCode SHA256 = 0x12
49-
toCode SHA512 = 0x13
50-
toCode SHA3 = 0x14
41+
toCode SHA1 = 0x11
42+
toCode SHA256 = 0x12
43+
toCode SHA512 = 0x13
44+
toCode SHA3 = 0x14
5145
toCode BLAKE2B = 0x40
5246
toCode BLAKE2S = 0x41
5347

54-
5548
encode :: HashAlgorithm -> Digest -> BL.ByteString
5649
encode h d = toLazyByteString $ encoder h d
5750

58-
5951
encoder :: HashAlgorithm -> Digest -> Builder
60-
encoder h d
61-
= (BB.word8 . fromIntegral $ toCode h)
62-
<> (BB.word8 . fromIntegral $ BS.length d)
63-
<> byteString d
64-
52+
encoder h d =
53+
(BB.word8 . fromIntegral $ toCode h) <>
54+
(BB.word8 . fromIntegral $ BS.length d) <>
55+
byteString d
6556

6657
decode :: BS.ByteString -> Either String MultihashDigest
6758
decode = parseOnly decoder
6859

69-
7060
decoder :: Parser MultihashDigest
7161
decoder = do
72-
h <- (fromCode . fromIntegral <$> A.anyWord8)
73-
l <- (fromIntegral <$> A.anyWord8)
74-
d <- A.take l
75-
return $ MultihashDigest h l d
62+
h <- (fromCode . fromIntegral <$> A.anyWord8)
63+
l <- (fromIntegral <$> A.anyWord8)
64+
d <- A.take l
65+
return $ MultihashDigest h l d

0 commit comments

Comments
 (0)