-- | Single-block implementation of XSHA-1.
--
-- See no reason to implement multiple blocks.
module Data.Digest.XSHA1 where

import Text.Printf
import Data.Vector (Vector)
import qualified Data.Vector as V
import Test.HUnit
import Data.Digest.XSHA1.Extend
import Data.Digest.XSHA1.Compress
import Numeric.Taint.Word32

-- | Single block of XSHA1.
xsha1 :: Vector N -- ^ 16 32-bit DWORDs.
      -> Regs
xsha1 d = finalize $ foldl (iter (extend d)) consts [0..79]

-- | Print 512 bit hash in hex.
hexp :: Regs -> IO ()
hexp (N a,N b,N c,N d,N e) = printf "%x %x %x %x %x\n" a b c d e


-- | Tests of known pairs, based on hash reverser's implementation.
test = runTestTT $ TestList 
    [ TestLabel "" $ TestCase testEmpty
    , TestLabel "a" $ TestCase testOneLetter 
    ]

testEmpty = assertEqual "for empty hash,"
    (N 0x4d3aa0ee, N 0x94261d5a, N 0x584a6f57, N 0x6b8d9960, N 0x1546c680)
    (xsha1 $ V.fromList [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0])
testOneLetter = assertEqual "for empty hash,"
    (N 0x4d3aa0ee, N 0x94261d5a, N 0x584a6f57, N 0x6b8d9960, N 0x1546c680)
    (xsha1 $ V.fromList [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0])