Safe Haskell | None |
---|---|
Language | Haskell2010 |
Data.LargeHashable.Class
Description
This module defines the central type class LargeHashable
of this package.
- class LargeHashable a where
- largeHash :: LargeHashable a => HashAlgorithm h -> a -> h
- class LargeHashable' t where
Documentation
class LargeHashable a where Source #
A type class for computing hashes (i.e. MD5, SHA256, ...) from haskell values.
The laws of this typeclass are the following:
- If two values are equal
according to
==
, then the finally computed hashes must also be equal according to==
. However it is not required that the hashes of inequal values have to be inequal. Also note that an instance ofLargeHashable
does not require a instance ofEq
. Using any sane algorithm the chance of a collision should be 1 / n where n is the number of different hashes possible. - If two values are inequal
according to
==
, then the probability of a hash collision is 1/n, where n is the number of possible hashes produced by the underlying hash algorithm.
A rule of thumb: hash all information that you would also need for serializing/deserializing values of your datatype. For instance, when hashing lists, you would not only hash the list elements but also the length of the list. Consider the following datatype
data Foo = Foo [Int] [Int]
We now write an instance for LargeHashable like this
instance LargeHashable Foo where updateHash (Foo l1 l2) = updateHash l1 >> updateHash l2
If we did not hash the length of a list, then the following two values
of Foo
would produce identical hashes:
Foo [1,2,3] [] Foo [1] [2,3]
Methods
updateHash :: a -> LH () Source #
updateHash :: (GenericLargeHashable (Rep a), Generic a) => a -> LH () Source #
Instances
largeHash :: LargeHashable a => HashAlgorithm h -> a -> h Source #
largeHash
is the central function of this package.
For a given value it computes a Hash
using the given
HashAlgorithm
.
class LargeHashable' t where Source #
Minimal complete definition
Methods
updateHash' :: LargeHashable a => t a -> LH () Source #