Permalink
Browse files

missing file from the repository for the last 6 months.

  • Loading branch information...
1 parent c15c446 commit 3e3b7b6cd7d0b35039cc4b0e2fb491f299676e9b @vincenthz committed Aug 15, 2011
Showing with 54 additions and 0 deletions.
  1. +54 −0 Crypto/Hash/SHA512t.hs
View
54 Crypto/Hash/SHA512t.hs
@@ -0,0 +1,54 @@
+{-# LANGUAGE MultiParamTypeClasses #-}
+
+-- |
+-- Module : Crypto.Hash.SHA512t
+-- License : BSD-style
+-- Maintainer : Vincent Hanquez <vincent@snarc.org>
+-- Stability : experimental
+-- Portability : unknown
+--
+-- A module containing SHA512/t
+--
+module Crypto.Hash.SHA512t
+ ( Ctx(..)
+ --, SHA512t
+
+ -- * Incremental hashing Functions
+ , init -- :: Ctx
+ , update -- :: Ctx -> ByteString -> Ctx
+ , finalize -- :: Ctx -> ByteString
+
+ -- * Single Pass hashing
+ , hash -- :: ByteString -> ByteString
+ , hashlazy -- :: ByteString -> ByteString
+ ) where
+
+import Prelude hiding (init)
+import Data.List (foldl')
+import Data.ByteString (ByteString)
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Lazy as L
+
+import qualified Crypto.Hash.SHA512 as SHA512
+
+data Ctx = Ctx !Int !SHA512.Ctx
+
+-- | init a context
+init :: Int -> Ctx
+init t = Ctx t (SHA512.init_t t)
+
+-- | update a context with a bytestring
+update :: Ctx -> ByteString -> Ctx
+update (Ctx t ctx) d = Ctx t (SHA512.update ctx d)
+
+-- | finalize the context into a digest bytestring
+finalize :: Ctx -> ByteString
+finalize (Ctx sz ctx) = B.take (sz `div` 8) (SHA512.finalize ctx)
+
+-- | hash a strict bytestring into a digest bytestring
+hash :: Int -> ByteString -> ByteString
+hash t = finalize . update (init t)
+
+-- | hash a lazy bytestring into a digest bytestring
+hashlazy :: Int -> L.ByteString -> ByteString
+hashlazy t = finalize . foldl' update (init t) . L.toChunks

0 comments on commit 3e3b7b6

Please sign in to comment.