Copyright | (C) Hécate Moonlight 2022 |
---|---|
License | BSD-3-Clause |
Maintainer | The Haskell Cryptography Group |
Portability | GHC only |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Synopsis
- data HashKey
- newHashKey :: IO HashKey
- data Hash
- hashByteString :: Maybe HashKey -> StrictByteString -> IO Hash
- data Multipart s
- withMultipart :: forall (a :: Type) (m :: Type -> Type). MonadIO m => Maybe HashKey -> (forall s. Multipart s -> m a) -> m Hash
- updateMultipart :: forall (m :: Type -> Type) (s :: Type). MonadIO m => Multipart s -> StrictByteString -> m ()
- hashToHexText :: Hash -> Text
- hashToHexByteString :: Hash -> StrictByteString
- hashToBinary :: Hash -> StrictByteString
Introduction
This API computes a fixed-length fingerprint for an arbitrarily long message. It is backed by the BLAKE2b algorithm.
Sample use cases:
- File integrity checking
- Creating unique identifiers to index arbitrarily long data
⚠️ Do not use this module to hash passwords! ⚠️ Please use the Sel.Hashing.Password module instead.
If you need to deviate from the defaults enforced by this module, please use the underlying bindings at LibSodium.Bindings.GenericHashing.
Hashing a message
The HashKey
is used to produce distinct fingerprints for the same message.
It is optional to use, and hashByteString
will always produce the same fingerprint
for the same message if a HashKey
is not given. This behaviour is similar to
MD5 and SHA-1 functions, for which hashByteString
is a faster and more secure alternative.
Create a new HashKey
with newHashKey
.
Since: 0.0.1.0
newHashKey :: IO HashKey Source #
Create a new HashKey
of size cryptoGenericHashKeyBytes
.
Since: 0.0.1.0
The fingerprint computed by hashByteString
.
It is produced by the BLAKE2b algorithm, and is
of size cryptoGenericHashBytes
, as recommended.
You can produce a human-readable string representation
of a Hash
by using the display
function.
Since: 0.0.1.0
Instances
Storable Hash Source # | Since: 0.0.1.0 |
Defined in Sel.Hashing | |
Show Hash Source # | Since: 0.0.1.0 |
Eq Hash Source # | Since: 0.0.1.0 |
Ord Hash Source # | Since: 0.0.1.0 |
Display Hash Source # | Since: 0.0.1.0 |
Defined in Sel.Hashing displayBuilder :: Hash -> Builder # displayList :: [Hash] -> Builder # displayPrec :: Int -> Hash -> Builder # |
hashByteString :: Maybe HashKey -> StrictByteString -> IO Hash Source #
Hash a StrictByteString
with the BLAKE2b algorithm, and an optional key.
Without a HashKey
, hashing the same data twice will give the same result.
Since: 0.0.1.0
Hashing a multi-part message
Multipart
is a cryptographic context for streaming hashing.
This API can be used when a message is too big to fit
in memory or when the message is received in portions.
Use it like this:
>>>
hashKey <- Hashing.newHashKey
>>>
hash <- Hashing.withMultipart (Just hashKey) $ \multipartState -> do -- we are in MonadIO
... message1 <- getMessage ... Hashing.updateMultipart multipartState message1 ... message2 <- getMessage ... Hashing.updateMultipart multipartState message2
Since: 0.0.1.0
:: forall (a :: Type) (m :: Type -> Type). MonadIO m | |
=> Maybe HashKey | Optional cryptographic key |
-> (forall s. Multipart s -> m a) | Continuation that gives you access to a |
-> m Hash |
Perform streaming hashing with a Multipart
cryptographic context.
If there is no HashKey
, you will get the same output for the same input all the time.
Use updateMultipart
within the continuation to add more message parts to be hashed.
The context is safely allocated first, then the continuation is run and then it is deallocated after that.
Since: 0.0.1.0
updateMultipart :: forall (m :: Type -> Type) (s :: Type). MonadIO m => Multipart s -> StrictByteString -> m () Source #
Conversion
hashToHexText :: Hash -> Text Source #
hashToHexByteString :: Hash -> StrictByteString Source #
Convert a Hash
to a strict, hexadecimal-encoded StrictByteString
.
Since: 0.0.1.0
hashToBinary :: Hash -> StrictByteString Source #
Convert a Hash
to a strict binary StrictByteString
.
Since: 0.0.1.0