| 1 |
{-# LANGUAGE BangPatterns #-} |
| 2 |
{-# LANGUAGE Trustworthy #-} |
| 3 |
|
| 4 |
-- | |
| 5 |
-- Module : Crypto.Hash.SHA256 |
| 6 |
-- License : BSD-3 |
| 7 |
-- Maintainer : Herbert Valerio Riedel <[email protected]> |
| 8 |
-- Stability : stable |
| 9 |
-- |
| 10 |
-- A module containing <https://en.wikipedia.org/wiki/SHA-2 SHA-256> bindings |
| 11 |
-- |
| 12 |
module Crypto.Hash.SHA256 |
| 13 |
( |
| 14 |
|
| 15 |
-- * Incremental API |
| 16 |
-- |
| 17 |
-- | This API is based on 4 different functions, similar to the |
| 18 |
-- lowlevel operations of a typical hash: |
| 19 |
-- |
| 20 |
-- - 'init': create a new hash context |
| 21 |
-- - 'update': update non-destructively a new hash context with a strict bytestring |
| 22 |
-- - 'updates': same as update, except that it takes a list of strict bytestrings |
| 23 |
-- - 'finalize': finalize the context and returns a digest bytestring. |
| 24 |
-- |
| 25 |
-- all those operations are completely pure, and instead of |
| 26 |
-- changing the context as usual in others language, it |
| 27 |
-- re-allocates a new context each time. |
| 28 |
-- |
| 29 |
-- Example: |
| 30 |
-- |
| 31 |
-- > import qualified Data.ByteString |
| 32 |
-- > import qualified Crypto.Hash.SHA256 as SHA256 |
| 33 |
-- > |
| 34 |
-- > main = print digest |
| 35 |
-- > where |
| 36 |
-- > digest = SHA256.finalize ctx |
| 37 |
-- > ctx = foldl SHA256.update ctx0 (map Data.ByteString.pack [ [1,2,3], [4,5,6] ]) |
| 38 |
-- > ctx0 = SHA256.init |
| 39 |
|
| 40 |
Ctx(..) |
| 41 |
, init -- :: Ctx |
| 42 |
, update -- :: Ctx -> ByteString -> Ctx |
| 43 |
, updates -- :: Ctx -> [ByteString] -> Ctx |
| 44 |
, finalize -- :: Ctx -> ByteString |
| 45 |
, finalizeAndLength -- :: Ctx -> (ByteString,Word64) |
| 46 |
, start -- :: ByteString -> Ct |
| 47 |
, startlazy -- :: L.ByteString -> Ctx |
| 48 |
|
| 49 |
-- * Single Pass API |
| 50 |
-- |
| 51 |
-- | This API use the incremental API under the hood to provide |
| 52 |
-- the common all-in-one operations to create digests out of a |
| 53 |
-- 'ByteString' and lazy 'L.ByteString'. |
| 54 |
-- |
| 55 |
-- - 'hash': create a digest ('init' + 'update' + 'finalize') from a strict 'ByteString' |
| 56 |
-- - 'hashlazy': create a digest ('init' + 'update' + 'finalize') from a lazy 'L.ByteString' |
| 57 |
-- - 'hashlazyAndLength': create a digest ('init' + 'update' + 'finalizeAndLength') from a lazy 'L.ByteString' |
| 58 |
-- |
| 59 |
-- Example: |
| 60 |
-- |
| 61 |
-- > import qualified Data.ByteString |
| 62 |
-- > import qualified Crypto.Hash.SHA256 as SHA256 |
| 63 |
-- > |
| 64 |
-- > main = print $ SHA256.hash (Data.ByteString.pack [0..255]) |
| 65 |
-- |
| 66 |
-- __NOTE__: The returned digest is a binary 'ByteString'. For |
| 67 |
-- converting to a base16/hex encoded digest the |
| 68 |
-- <https://hackage.haskell.org/package/base16-bytestring base16-bytestring> |
| 69 |
-- package is recommended. |
| 70 |
|
| 71 |
, hash -- :: ByteString -> ByteString |
| 72 |
, hashlazy -- :: L.ByteString -> ByteString |
| 73 |
, hashlazyAndLength -- :: L.ByteString -> (ByteString,Int64) |
| 74 |
|
| 75 |
-- ** HMAC-SHA-256 |
| 76 |
-- |
| 77 |
-- | <https://tools.ietf.org/html/rfc2104 RFC2104>-compatible |
| 78 |
-- <https://en.wikipedia.org/wiki/HMAC HMAC>-SHA-256 digests |
| 79 |
|
| 80 |
, hmac -- :: ByteString -> ByteString -> ByteString |
| 81 |
, hmaclazy -- :: ByteString -> L.ByteString -> ByteString |
| 82 |
, hmaclazyAndLength -- :: ByteString -> L.ByteString -> (ByteString,Word64) |
| 83 |
|
| 84 |
-- ** HKDF-SHA-256 |
| 85 |
-- |
| 86 |
-- | <https://tools.ietf.org/html/rfc5869 RFC5869>-compatible |
| 87 |
-- <https://en.wikipedia.org/wiki/HKDF HKDF>-SHA-256 key derivation function |
| 88 |
|
| 89 |
, hkdf |
| 90 |
) where |
| 91 |
|
| 92 |
import Prelude () |
| 93 |
import Crypto.Hash.SHA256.Legacy |