packages feed

cryptohash 0.6.2 → 0.6.3

raw patch · 6 files changed

+133/−1 lines, 6 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Crypto.Hash.SHA512: init_t :: Int -> Ctx
+ Crypto.Hash.SHA512t: Ctx :: !Int -> !Ctx -> Ctx
+ Crypto.Hash.SHA512t: data Ctx
+ Crypto.Hash.SHA512t: finalize :: Ctx -> ByteString
+ Crypto.Hash.SHA512t: hash :: Int -> ByteString -> ByteString
+ Crypto.Hash.SHA512t: hashlazy :: Int -> ByteString -> ByteString
+ Crypto.Hash.SHA512t: init :: Int -> Ctx
+ Crypto.Hash.SHA512t: update :: Ctx -> ByteString -> Ctx

Files

Crypto/Hash/SHA512.hs view
@@ -15,6 +15,7 @@  	-- * Incremental hashing Functions 	, init     -- :: Ctx+	, init_t   -- :: Int -> Ctx 	, update   -- :: Ctx -> ByteString -> Ctx 	, finalize -- :: Ctx -> ByteString @@ -72,6 +73,9 @@ foreign import ccall unsafe "sha512.h sha512_init" 	c_sha512_init :: Ptr Ctx -> IO () +foreign import ccall unsafe "sha512.h sha512_init_t"+	c_sha512_init_t :: Ptr Ctx -> Int -> IO ()+ foreign import ccall "sha512.h sha512_update" 	c_sha512_update :: Ptr Ctx -> CString -> Word32 -> IO () @@ -96,6 +100,11 @@ -- | init a context init :: Ctx init = unsafePerformIO $ allocInternal $ \ptr -> do (c_sha512_init ptr >> peek ptr)++{-# NOINLINE init_t #-}+-- | init a context using FIPS 180-4 for truncated SHA512+init_t :: Int -> Ctx+init_t t = unsafePerformIO $ allocInternal $ \ptr -> do (c_sha512_init_t ptr t >> peek ptr)  {-# NOINLINE update #-} -- | update a context with a bytestring
+ Crypto/Hash/SHA512t.hs view
@@ -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
Tests.hs view
@@ -11,6 +11,7 @@ import qualified Crypto.Hash.SHA256 as SHA256 import qualified Crypto.Hash.SHA384 as SHA384 import qualified Crypto.Hash.SHA512 as SHA512+import qualified Crypto.Hash.SHA512t as SHA512t import qualified Crypto.Hash.RIPEMD160 as RIPEMD160 import qualified Crypto.Hash.Tiger as Tiger import qualified Crypto.Hash.Skein256 as Skein256@@ -30,11 +31,17 @@ md2Hash    = HashFct { fctHash = MD2.hash, fctInc = hashinc MD2.init MD2.update MD2.finalize } md4Hash    = HashFct { fctHash = MD4.hash, fctInc = hashinc MD4.init MD4.update MD4.finalize } md5Hash    = HashFct { fctHash = MD5.hash, fctInc = hashinc MD5.init MD5.update MD5.finalize }+ sha1Hash   = HashFct { fctHash = SHA1.hash, fctInc = hashinc SHA1.init SHA1.update SHA1.finalize }+ sha224Hash = HashFct { fctHash = SHA224.hash, fctInc = hashinc SHA224.init SHA224.update SHA224.finalize } sha256Hash = HashFct { fctHash = SHA256.hash, fctInc = hashinc SHA256.init SHA256.update SHA256.finalize }+ sha384Hash = HashFct { fctHash = SHA384.hash, fctInc = hashinc SHA384.init SHA384.update SHA384.finalize } sha512Hash = HashFct { fctHash = SHA512.hash, fctInc = hashinc SHA512.init SHA512.update SHA512.finalize }+sha512_224Hash = HashFct { fctHash = SHA512t.hash 224, fctInc = hashinc (SHA512t.init 224) SHA512t.update SHA512t.finalize }+sha512_256Hash = HashFct { fctHash = SHA512t.hash 256, fctInc = hashinc (SHA512t.init 256) SHA512t.update SHA512t.finalize }+ ripemd160Hash = HashFct { fctHash = RIPEMD160.hash, fctInc = hashinc RIPEMD160.init RIPEMD160.update RIPEMD160.finalize } tigerHash = HashFct { fctHash = Tiger.hash, fctInc = hashinc Tiger.init Tiger.update Tiger.finalize } @@ -75,6 +82,15 @@ 		"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", 		"07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6", 		"3eeee1d0e11733ef152a6c29503b3ae20c4f1f3cda4cb26f1bc1a41f91c7fe4ab3bd86494049e201c4bd5155f31ecb7a3c8606843c4cc8dfcab7da11c8ae5045" ]),++	("SHA512/224", sha512_224Hash, [+		"6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4",+		"944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37",+		"2b9d6565a7e40f780ba8ab7c8dcf41e3ed3b77997f4c55aa987eede5" ]),+	("SHA512/256", sha512_256Hash, [+		"c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a",+		"dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d",+		"cc8d255a7f2f38fd50388fd1f65ea7910835c5c1e73da46fba01ea50d5dd76fb" ]), 	("RIPEMD160", ripemd160Hash, [ 		"9c1185a5c5e9fc54612808977ee8f548b2258d31", 		"37f332f68db77bd9d7edd4969571ad671cf9dd3b",
cbits/sha512.c view
@@ -193,3 +193,53 @@ 	for (i = 0; i < 8; i++) 		p[i] = cpu_to_be64(ctx->h[i]); }++#include <stdio.h>++void sha512_init_t(struct sha512_ctx *ctx, int t)+{+	memset(ctx, 0, sizeof(*ctx));+	if (t >= 512)+		return;++	switch (t) {+	case 224:+		ctx->h[0] = 0x8c3d37c819544da2ULL;+		ctx->h[1] = 0x73e1996689dcd4d6ULL;+		ctx->h[2] = 0x1dfab7ae32ff9c82ULL;+		ctx->h[3] = 0x679dd514582f9fcfULL;+		ctx->h[4] = 0x0f6d2b697bd44da8ULL;+		ctx->h[5] = 0x77e36f7304c48942ULL;+		ctx->h[6] = 0x3f9d85a86a1d36c8ULL;+		ctx->h[7] = 0x1112e6ad91d692a1ULL;+		break;+	case 256:+		ctx->h[0] = 0x22312194fc2bf72cULL;+		ctx->h[1] = 0x9f555fa3c84c64c2ULL;+		ctx->h[2] = 0x2393b86b6f53b151ULL;+		ctx->h[3] = 0x963877195940eabdULL;+		ctx->h[4] = 0x96283ee2a88effe3ULL;+		ctx->h[5] = 0xbe5e1e2553863992ULL;+		ctx->h[6] = 0x2b0199fc2c85b8aaULL;+		ctx->h[7] = 0x0eb72ddc81c52ca2ULL;+		break;+	default: {+		char buf[8+4];+		uint8_t out[64];+		int i;++		sha512_init(ctx);+		for (i = 0; i < 8; i++)+			ctx->h[i] ^= 0xa5a5a5a5a5a5a5a5ULL;++		i = sprintf(buf, "SHA-512/%d", t);+		sha512_update(ctx, buf, i);+		sha512_finalize(ctx, out);++		/* re-init the context, otherwise len is changed */+		memset(ctx, 0, sizeof(*ctx));+		for (i = 0; i < 8; i++)+			ctx->h[i] = cpu_to_be64(((uint64_t *) out)[i]);+		}+	}+}
cbits/sha512.h view
@@ -49,4 +49,6 @@ void sha512_update(struct sha512_ctx *ctx, uint8_t *data, uint32_t len); void sha512_finalize(struct sha512_ctx *ctx, uint8_t *out); +void sha512_init_t(struct sha512_ctx *ctx, int t);+ #endif
cryptohash.cabal view
@@ -1,5 +1,5 @@ Name:                cryptohash-Version:             0.6.2+Version:             0.6.3 Description:     A collection of crypto hashes, with a practical incremental and one-pass, pure APIs,     with performance close to the fastest implementations available in others languages.@@ -43,6 +43,7 @@                      Crypto.Hash.SHA256                      Crypto.Hash.SHA384                      Crypto.Hash.SHA512+                     Crypto.Hash.SHA512t                      Crypto.Hash.MD2                      Crypto.Hash.MD4                      Crypto.Hash.MD5