C# Class StopGuessing.EncryptionPrimitives.UniversalHashFunction

A keyed universal hash function is a function that produces up to 32-bits that meet the definition of a universal hash (and an additional 32 bits that do not meet the requirement and so, in theory could be biased), guaranteeing that: (1) an attacker cannot construct inputs that are likely to have a bias of 0/1 at any bit position (2) an attacker cannot construct two values that will collide with a greater-than-random chance so long as that attacker (a) does not know the key, and (b) is unable to observe the outputs of the function. In contrast, standard hash functions do not guarantee that attackers cannot easily generate collisions, / even if the application using the function prefixes or postfixes the value to be hashed with a key. This leaves systems that use standard (non-universal) hash functions vulnerable to algorithmic complexity attacks. See: https://www.usenix.org/legacy/events/sec03/tech/full_papers/crosby/crosby.pdf When generating a hash, the lower 32 bits generated by the function should be thrown out unless you are certain that using bits that may be subject to attacker bias will cause no harm. For example, for a hash ring that uses all 64-bit points, where the first 32 are the universal hash bits, collisions should be strictly less likely than in a ring that only uses the 32 safe bits---though your ring entries will require twice as much space.) An instance of a universal hash function is re-entrant so, once an instance has been constructed, the hash function can be safely called from multiple threads to perform more than one hash at a time. For more information on the construction used in this implementation, see https://en.wikipedia.org/wiki/Universal_hashing (this construction is the last one in the "hashing vectors" section, labled with the superscript "strong") and http://comjnl.oxfordjournals.org/content/57/11/1624.full.pdf (this is the simple "Multilinear" construction)
ファイルを表示 Open project: Microsoft/StopGuessing Class Usage Examples

Public Methods

Method Description
Hash ( IList messageToBeHashed, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong

Hash an array of 32 bit values.

Hash ( IList messageToBeHashed, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong

Hash an array of bytes.

Hash ( Int32 valueToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong

Hash a value.

Hash ( System.Int64 valueToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong

Hash a value.

Hash ( UInt32 valueToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong

Hash a value.

Hash ( string stringToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong

Hash a string.

Hash ( ulong valueToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong

Hash a value.

UniversalHashFunction ( byte keyOf16Or24Or32Bytes, int randomKeyVectorLengthInBytes = 256 ) : System

Construct a keyed universal hash function. An instance of a universal hash function needs to store on the order of RandomKeyVectorLengthInBytes values upon construction, so it is best not to initialize this function for large strings (e.g., those over 32k).

UniversalHashFunction ( int randomKeyVectorLengthInBytes ) : System

Construct a random universal hash function

UniversalHashFunction ( string key, int randomKeyVectorLengthInBytes = 256 ) : System

Private Methods

Method Description
HashArrayThatExceedsLengthOfRandomKeyVector ( IList messageToBeHashed, int numberOfBitsToReturn ) : ulong

When asked to hash a value that exceeds the length of the key vector, we break the message into blocks no larger than the hash of the key vector, hash each block, and then recursively hash the array containing the hash of each block. (If the message is so long that the array of block hashes exceeds the length of the key vector, we'll do so again yet again.)

HashArrayThatExceedsLengthOfRandomKeyVector ( IList messageToBeHashed, int numberOfBitsToReturn ) : ulong
SetVectorLength ( int randomKeyVectorLengthInBytes ) : void
SetVectorLengthAndGetNumberOfRandomBytesNeeded ( int randomKeyVectorLengthInBytes ) : int

Method Details

Hash() public method

Hash an array of 32 bit values.
public Hash ( IList messageToBeHashed, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong
messageToBeHashed IList An array of 32 bit unsigned values.
numberOfBitsToReturn int The number of hash bits to return. /// Use a value ≤ 32 to ensure the result meets the requirements of a unviversal hash. /// If you choose a value between 33 and 64, only the high-order 32 bits are guaranteed to be unbiased.
return ulong

Hash() public method

Hash an array of bytes.
public Hash ( IList messageToBeHashed, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong
messageToBeHashed IList An array of bytes.
numberOfBitsToReturn int The number of hash bits to return. /// Use a value ≤ 32 to ensure the result meets the requirements of a unviversal hash. /// If you choose a value between 33 and 64, only the high-order 32 bits are guaranteed to be unbiased.
return ulong

Hash() public method

Hash a value.
public Hash ( Int32 valueToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong
valueToHash System.Int32 The value to be hashed.
numberOfBitsToReturn int The number of hash bits to return. /// Use a value ≤ 32 to ensure the result meets the requirements of a unviversal hash. /// If you choose a value between 33 and 64, only the high-order 32 bits are guaranteed to be unbiased.
return ulong

Hash() public method

Hash a value.
public Hash ( System.Int64 valueToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong
valueToHash System.Int64 The value to be hashed.
numberOfBitsToReturn int The number of hash bits to return. /// Use a value ≤ 32 to ensure the result meets the requirements of a unviversal hash. /// If you choose a value between 33 and 64, only the high-order 32 bits are guaranteed to be unbiased.
return ulong

Hash() public method

Hash a value.
public Hash ( UInt32 valueToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong
valueToHash System.UInt32 The value to be hashed.
numberOfBitsToReturn int The number of hash bits to return. /// Use a value ≤ 32 to ensure the result meets the requirements of a unviversal hash. /// If you choose a value between 33 and 64, only the high-order 32 bits are guaranteed to be unbiased.
return ulong

Hash() public method

Hash a string.
public Hash ( string stringToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong
stringToHash string A string to hash, which will be converted to a UTF8 byte array for hashing.
numberOfBitsToReturn int The number of hash bits to return. /// Use a value ≤ 32 to ensure the result meets the requirements of a unviversal hash. /// If you choose a value between 33 and 64, only the high-order 32 bits are guaranteed to be unbiased.
return ulong

Hash() public method

Hash a value.
public Hash ( ulong valueToHash, int numberOfBitsToReturn = MaximumNumberOfResultBitsGuaranteedToBeUnbiased ) : ulong
valueToHash ulong The value to be hashed.
numberOfBitsToReturn int The number of hash bits to return. /// Use a value ≤ 32 to ensure the result meets the requirements of a unviversal hash. /// If you choose a value between 33 and 64, only the high-order 32 bits are guaranteed to be unbiased.
return ulong

UniversalHashFunction() public method

Construct a keyed universal hash function. An instance of a universal hash function needs to store on the order of RandomKeyVectorLengthInBytes values upon construction, so it is best not to initialize this function for large strings (e.g., those over 32k).
public UniversalHashFunction ( byte keyOf16Or24Or32Bytes, int randomKeyVectorLengthInBytes = 256 ) : System
keyOf16Or24Or32Bytes byte A key that should not be known to those who might try to create collisions, provided as an array of 16, 24, or 32 bytes.
randomKeyVectorLengthInBytes int The length of the random key vector used for hashing. Should be twice the length of the longest value you expect to hash, /// unless you hash values bigger than you would want to keep in memory. If hashing a value longer than this, the universal hash properties may not hold.
return System

UniversalHashFunction() public method

Construct a random universal hash function
public UniversalHashFunction ( int randomKeyVectorLengthInBytes ) : System
randomKeyVectorLengthInBytes int The length of the random key vector used for hashing. Should be twice the length of the longest value you expect to hash, /// unless you hash values bigger than you would want to keep in memory. If hashing a value longer than this, the universal hash properties may not hold.
return System

UniversalHashFunction() public method

public UniversalHashFunction ( string key, int randomKeyVectorLengthInBytes = 256 ) : System
key string A key that should not be known to those who might try to create collisions, provided as a string.
randomKeyVectorLengthInBytes int The number of random bytes to generate, which should be equal /// to half the maximum allowable length, in bytes, of any value to be hashed.
return System