Skip to content

Commit

Permalink
moved textDigest to the library; uploaded blakesum 0.3 to hackage
Browse files Browse the repository at this point in the history
  • Loading branch information
killerswan committed Jul 18, 2011
1 parent d5b1cb3 commit 8a9d06e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
29 changes: 28 additions & 1 deletion Data/Digest/SHA3/Candidate/BLAKE.hs
Expand Up @@ -8,7 +8,17 @@
--
-- | BLAKE is one of the finalists in the NIST SHA-3 hash function competition
-- to replace SHA-1 and SHA-2.
module Data.Digest.SHA3.Candidate.BLAKE ( blake256, blake512, blake224, blake384 ) where
module Data.Digest.SHA3.Candidate.BLAKE (

-- * Digests
blake256
, blake512
, blake224
, blake384

-- * Simple display
, textDigest
) where


import Data.Bits
Expand All @@ -17,6 +27,8 @@ import Data.Int
import Data.List
import qualified Data.ByteString.Lazy as B
import qualified Data.Vector.Storable as V
import qualified Data.Text.Lazy as T
import qualified Text.Printf as P
--import Control.Parallel.Strategies


Expand Down Expand Up @@ -511,3 +523,18 @@ blake384 salt message =
B.take 48 $ blake config salt message



-- | Convert a digest (or other ByteString) to hexadecimal digits
-- | For example, to compute a digest of a message, "0x00",
-- using BLAKE-512 (faster on 64 bit systems),
-- with a salt of 0, and get the digits in hex:
--
-- > import Data.Digest.SHA3.Candidate.BLAKE
-- > import qualified Data.ByteString.Lazy as B
-- >
-- > textDigest $ blake512 (B.take 32 $ B.repeat 0) (B.singleton 0)
textDigest :: B.ByteString -> T.Text
textDigest digest =
T.pack $ (P.printf "%02x") =<< B.unpack digest


7 changes: 5 additions & 2 deletions blakesum.cabal
@@ -1,5 +1,5 @@
name: blakesum
version: 0.2
version: 0.3
synopsis: The BLAKE SHA-3 candidate hashes, in Haskell
description:
.
Expand All @@ -24,7 +24,10 @@ build-type: Simple
cabal-version: >=1.6

library
build-depends: base >= 4 && < 5, vector >= 0 && < 1, bytestring >= 0 && < 1
build-depends: base >= 4 && < 5,
vector >= 0 && < 1,
bytestring >= 0 && < 1,
text >= 0 && < 1
exposed-modules: Data.Digest.SHA3.Candidate.BLAKE
ghc-options: -Wall -O2

Expand Down
8 changes: 1 addition & 7 deletions demo/Main.hs
Expand Up @@ -15,7 +15,6 @@ import Data.Digest.SHA3.Candidate.BLAKE
import qualified Data.ByteString.Lazy as B
import System
import IO
import Text.Printf
import System.Console.GetOpt
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.Encoding as E
Expand Down Expand Up @@ -77,7 +76,7 @@ options = [ Option "a" ["algorithm"]
, Option "v" ["version"]
(NoArg $ \_ -> do
me <- getProgName
hPutStrLn stderr $ me ++ " version 0.0"
hPutStrLn stderr $ me ++ " version 0.3"
exitWith ExitSuccess)
"display version and exit"
]
Expand Down Expand Up @@ -106,11 +105,6 @@ fileMap :: ( B.ByteString -> IO () ) -> [FilePath] -> IO ()
fileMap f paths = fileMapWithPath (\_ -> f) paths


-- convert a digest into text
textDigest :: B.ByteString -> T.Text
textDigest digest =
T.pack $ (printf "%02x") =<< B.unpack digest


-- compute a hash, return text
getHash256 :: B.ByteString -> B.ByteString -> T.Text
Expand Down
6 changes: 6 additions & 0 deletions demo/profile.bash
@@ -1,4 +1,5 @@
#!/bin/bash
# TODO: NEED TO FIX THIS SO IT STILL PROFILES THE LIBRARY, TOO...

EXEBASE=blakesum
EXE="$EXEBASE"
Expand All @@ -12,6 +13,11 @@ function qrm() {
qrm "$EXE"
qrm "$EXE"TS

cd ..
cabal configure && cabal build && cabal install && cabal install -p
cd -


# FILE TO TEST
#FILE="testheap.data" # about 840 megabytes
#FILE="C:\Users\Kevin\Desktop\Next_700.pdf"
Expand Down
12 changes: 11 additions & 1 deletion test/Main.hs
Expand Up @@ -11,10 +11,11 @@
import Test.HUnit
import Data.Digest.SHA3.Candidate.BLAKE
import qualified Data.ByteString.Lazy as B
import Data.Text.Lazy as T


zeroByteString :: Int -> B.ByteString
zeroByteString n = B.pack $ take n $ repeat 0
zeroByteString n = B.pack $ Prelude.take n $ Prelude.repeat 0

test_blake256 :: Test
test_blake256 =
Expand Down Expand Up @@ -80,11 +81,20 @@ test_blake224 =
(blake224 (zeroByteString 16) (zeroByteString 72))


test_textDigest :: Test
test_textDigest =
TestCase $ do
assertEqual "textDigest of 0xa080cb00987"
(T.pack "080cb00987")
(textDigest $ B.pack [0xa08, 0x0c, 0xb0, 0x09, 0x87])


tests :: Test
tests = TestList [ "BLAKE-256" ~: test_blake256
, "BLAKE-512" ~: test_blake512
, "BLAKE-224" ~: test_blake224
, "BLAKE-384" ~: test_blake384
, "textDigest" ~: test_textDigest
]


Expand Down

0 comments on commit 8a9d06e

Please sign in to comment.