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

Commit

Permalink
Prepping for release.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehoersten committed Apr 5, 2015
1 parent 0badad5 commit 4b54322
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
42 changes: 29 additions & 13 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Data.Byteable (toBytes)
import Data.ByteString (ByteString)
import Data.ByteString.Lazy (fromStrict, toStrict)
import Options.Applicative
import System.Exit (exitFailure, exitSuccess)

import System.IO.Streams (InputStream, stdin, stdout,
withFileAsInput, write)
Expand All @@ -23,40 +24,46 @@ data Config =
{ cfFile :: Maybe FilePath
, cfAlgo :: MH.HashAlgorithm
, cfBase :: MB.BaseEncoding
, cfHash :: Maybe MH.Digest
, cfTerm :: Termination
} deriving Show


main :: IO ()
main = do
-- TODO add file checking
config <- execParser opts

digest <- maybe
(hash (cfAlgo config) stdin)
(`withFileAsInput` hash (cfAlgo config))
(cfFile config)

write (encode config digest) stdout
digest <- maybe (hashStdin config) (hashFile config) $ cfFile config
write (multihash config digest) stdout
where
encode (Config _file algo base term) =
hashStdin config = hash (cfAlgo config) stdin
hashFile config file = withFileAsInput file . hash $ cfAlgo config
multihash (Config _file algo base _hash term) =
Just . toStrict . line term . MB.encode base . MH.encode algo

line Null = (<> "\0")
line Newline = (<> "\n")


-- TODO add BLAKE support
hash :: MH.HashAlgorithm -> InputStream ByteString -> IO MH.Digest
hash MH.SHA1 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA1))
hash MH.SHA256 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA256))
hash MH.SHA512 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA512))
hash MH.SHA3 is = toBytes <$> (hashInputStream is :: IO (Digest CH.SHA3_256))
hash MH.BLAKE2B _ = undefined
hash MH.BLAKE2S _ = undefined
hash MH.BLAKE2B _ = undefined
hash MH.BLAKE2S _ = undefined


opts :: ParserInfo Config
opts = info
(helper <*> (Config <$> fileArg <*> algoOpt <*> baseOpt <*> nullTermFlag))
(helper <*> (Config
<$> fileArg
<*> algoOpt
<*> baseOpt
<*> checkOpt
<*> nullTermFlag
))
(fullDesc
<> header "Generate a multihash for the given input."
<> progDesc "Hash from FILE or stdin if not given.")
Expand All @@ -69,7 +76,7 @@ algoOpt =
<> short 'a'
<> metavar "ALGO"
<> showDefault <> value MH.SHA256
<> help ("Hash algorithm to apply to input " <> show ([minBound..] :: [MH.HashAlgorithm]))
<> help ("Hash algorithm to apply to input, ignored if checking hash " <> show ([minBound..] :: [MH.HashAlgorithm]))


baseOpt :: Parser MB.BaseEncoding
Expand All @@ -79,7 +86,16 @@ baseOpt =
<> short 'e'
<> metavar "ENCODING"
<> showDefault <> value MB.Base58
<> help ("Base encoding of output digest " <> show ([minBound..] :: [MB.BaseEncoding]))
<> help ("Base encoding of output digest, ignored if checking hash " <> show ([minBound..] :: [MB.BaseEncoding]))


checkOpt :: Parser (Maybe MH.Digest)
checkOpt =
optional . option auto
$ long "check"
<> short 'c'
<> metavar "DIGEST"
<> help "Check for matching digest"


nullTermFlag :: Parser Termination
Expand Down
21 changes: 13 additions & 8 deletions multihash.cabal
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
name: multihash
version: 0.1.0
-- synopsis:
-- description:
synopsis: Multihash library and CLI executable
description: Multihash is a protocol for encoding the hash algorithm
and digest length at the start of the digest.
More information available at https:\/\/github.com\/jbenet\/multihash\/.
.
Base32 encoding, Blake hashing, and file checking still to be added.
.
license: BSD3
license-file: LICENSE
author: Luke Hoersten
maintainer: [email protected]
-- copyright:
copyright: 2015 Luke Hoersten
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10

source-repository head
type: git
location: git://github.com/LukeHoersten/multihash.git


library
exposed-modules: Data.Multihash.Base
, Data.Multihash.Digest
, System.IO.Streams.Crypto


-- other-modules:
-- other-extensions:
hs-source-dirs: src
default-language: Haskell2010

Expand All @@ -38,7 +44,6 @@ executable multihash
main-is: Main.hs
hs-source-dirs: main
default-language: Haskell2010
ghc-prof-options: -fprof-auto "-with-rtsopts=-N -p -s -h -i0.1 -xc"
ghc-options: -threaded -Wall -fwarn-tabs -funbox-strict-fields -O2 -fdicts-cheap
-fno-warn-orphans -fno-warn-unused-do-bind -rtsopts

Expand Down
8 changes: 4 additions & 4 deletions src/Data/Multihash/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import Data.Either (Either)
import qualified Data.Hex as B16


-- TODO add Base32 encoding
data BaseEncoding
= Base2
| Base16 -- Hex
-- | Base32
| Base58 -- BTC
= Base2 -- ^ Raw binary encoding
| Base16 -- ^ Hexadecimal encoding
| Base58 -- ^ Bitcoin encoding
| Base64
deriving (Show, Read, Eq, Enum, Bounded)

Expand Down

0 comments on commit 4b54322

Please sign in to comment.