Hash functions

A hash function is a function that takes an input (or "message") and returns a fixed-size string of bytes, typically a hash code. The output is usually a seemingly random string of characters, but the same input will always yield the same hash code. Hash functions are commonly used in data structures like hash tables, for cryptography, or for verifying data integrity.

Key properties of a hash function:

  • Deterministic: Same input should always produce the same output.
  • Fast computation: The hash value for any input should be quick to compute.
  • Uniformity: The output should be uniformly distributed across the possible hash values.
  • Collision-resistant: It should be difficult to find two distinct inputs that produce the same hash output.

Basic Quick Sort

function simpleHash(str) {
  let hash = 0;
  for (let i = 0; i < str.length; i++) {
    hash = (hash << 5) - hash + str.charCodeAt(i);
    hash |= 0; // Convert to 32bit integer
  }
  return hash;
}