Hashing Strings in JavaScript

Hashing strings is a common task in programming, and JavaScript provides several ways to achieve this. In this tutorial, we’ll explore how to generate a hash from a string in JavaScript.

Introduction to Hashing

Hashing is the process of converting input data of any size into a fixed-size string of characters, known as a hash value or digest. This is useful for storing and comparing data efficiently, such as passwords or file contents. There are many types of hashing algorithms, each with its strengths and weaknesses.

Simple Hash Function

One simple way to hash a string in JavaScript is by using the following function:

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

This function takes a string as input and returns a hashed value. It works by iterating over each character in the string, converting it to its ASCII code, and then applying a series of bitwise operations to generate the hash.

Improved Hash Function: cyrb53

The simple hash function above is not suitable for all use cases, especially when dealing with large datasets or security-critical applications. A more robust alternative is the cyrb53 hash function:

const cyrb53 = (str, seed = 0) => {
  let h1 = 0xdeadbeef ^ seed;
  let h2 = 0x41c6ce57 ^ seed;
  for (let i = 0; i < str.length; i++) {
    const ch = str.charCodeAt(i);
    h1 = Math.imul(h1 ^ ch, 2654435761);
    h2 = Math.imul(h2 ^ ch, 1597334677);
  }
  h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
  h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
  h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
  h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
  return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};

This function generates a 53-bit hash value and is designed to provide good distribution and low collision rates. It’s also relatively fast and suitable for most use cases.

TinySimpleHash

For situations where a very simple and lightweight hash function is needed, consider the TinySimpleHash:

const TSH = s => {
  let h = 9;
  for (let i = 0; i < s.length; i++) {
    h = Math.imul(h ^ s.charCodeAt(i), 9 ** 9);
  }
  return h ^ (h >>> 9);
};

This function is extremely compact and still provides reasonable hash quality, although it’s not suitable for security-critical applications.

Example Usage

To demonstrate how to use these hash functions, consider the following example:

const str = "Hello, World!";
console.log(hashCode(str)); // Simple hash function
console.log(cyrb53(str)); // cyrb53 hash function
console.log(TSH(str)); // TinySimpleHash

These examples illustrate how to generate a hash value from a string using different algorithms.

Conclusion

In conclusion, hashing strings in JavaScript can be achieved through various methods, each with its trade-offs between simplicity, performance, and security. By understanding the basics of hashing and selecting an appropriate algorithm for your use case, you can efficiently store and compare data in your applications.

Leave a Reply

Your email address will not be published. Required fields are marked *