File: SHA3.hs

package info (click to toggle)
haskell-cryptohash 0.11.9-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 336 kB
  • sloc: haskell: 1,325; ansic: 991; makefile: 4
file content (81 lines) | stat: -rw-r--r-- 2,303 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-- |
-- Module      : Crypto.MAC.SHA3
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
-- provide a simple SHA3 MAC mechanism with
--
-- > mac = hash(key|message)
--
module Crypto.MAC.SHA3
    ( MAC512(..)
    , MAC384(..)
    , MAC256(..)
    , MAC224(..)
    , mac512
    , mac384
    , mac256
    , mac224
    ) where

import Data.Byteable
import Data.ByteString (ByteString)
import Crypto.Hash

-- | SHA3_512 MAC
data MAC512 = MAC512 { getDigest512 :: Digest SHA3_512 }

instance Byteable MAC512 where
    toBytes (MAC512 b) = toBytes b
instance Eq MAC512 where
    (MAC512 b1) == (MAC512 b2) = constEqBytes (toBytes b1) (toBytes b2)

-- | SHA3_384 MAC
data MAC384 = MAC384 { getDigest384 :: Digest SHA3_384 }

instance Byteable MAC384 where
    toBytes (MAC384 b) = toBytes b
instance Eq MAC384 where
    (MAC384 b1) == (MAC384 b2) = constEqBytes (toBytes b1) (toBytes b2)

-- | SHA3_256 MAC
data MAC256 = MAC256 { getDigest256 :: Digest SHA3_256 }

instance Byteable MAC256 where
    toBytes (MAC256 b) = toBytes b
instance Eq MAC256 where
    (MAC256 b1) == (MAC256 b2) = constEqBytes (toBytes b1) (toBytes b2)

-- | SHA3_224 MAC
data MAC224 = MAC224 { getDigest224 :: Digest SHA3_224 }

instance Byteable MAC224 where
    toBytes (MAC224 b) = toBytes b
instance Eq MAC224 where
    (MAC224 b1) == (MAC224 b2) = constEqBytes (toBytes b1) (toBytes b2)

-- | compute a MAC using a simple SHA3_512 key|msg
mac512 :: ByteString -- ^ secret
       -> ByteString -- ^ message
       -> MAC512
mac512 secret msg = MAC512 $ hashFinalize $ hashUpdates hashInit [secret,msg]

-- | compute a MAC using a simple SHA3_384 key|msg
mac384 :: ByteString -- ^ secret
       -> ByteString -- ^ message
       -> MAC384
mac384 secret msg = MAC384 $ hashFinalize $ hashUpdates hashInit [secret,msg]

-- | compute a MAC using a simple SHA3_256 key|msg
mac256 :: ByteString -- ^ secret
       -> ByteString -- ^ message
       -> MAC256
mac256 secret msg = MAC256 $ hashFinalize $ hashUpdates hashInit [secret,msg]

-- | compute a MAC using a simple SHA3_224 key|msg
mac224 :: ByteString -- ^ secret
       -> ByteString -- ^ message
       -> MAC224
mac224 secret msg = MAC224 $ hashFinalize $ hashUpdates hashInit [secret,msg]