A hash is a value generated from input data using a hashing algorithm. In JavaScript, you can create hashes from strings using simple custom algorithms or cryptographic algorithms such as SHA-256.
- Use custom hashing methods for simple identifiers or non-security-related purposes.
- Use Node.js crypto.createHash() for cryptographic hashing on the server.
- Use the crypto.subtle API to create secure hashes in modern browsers.
These are the following approaches to create a hash from a string in JavaScript.
Approach 1: Using the charCodeAt() Method
The charCodeAt() method returns the numeric Unicode value of a character at a specified index. These values can be combined using mathematical operations to generate a simple numeric hash.
function createHash(string) {
let hash = 0;
if (string.length === 0) {
return hash;
}
for (let i = 0; i < string.length; i++) {
const char = string.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash |= 0;
}
return hash;
}
const text = "GeeksforGeeks";
console.log(createHash(text));
Output
-1119635595
Approach 2: Using crypto.createHash() in Node.js
Node.js provides the built-in crypto module for creating cryptographic hashes. The createHash() method supports algorithms such as SHA-256 and SHA-512.
const crypto = require("crypto");
const text = "Geek";
const hash = crypto
.createHash("sha256")
.update(text)
.digest("hex");
console.log(hash);
Output
c8b223036b366f4a0268c970d6c2014bff774ebb5249e59ef99cc306cdb83e2e
Note: SHA-256 is generally preferred over older algorithms such as MD5 and SHA-1 for security-related use cases.
Approach 3: Using the reduce() Method
The reduce() method can process every character of a string and combine their character codes into a single numeric value.
function createHash(string) {
return [...string].reduce((hash, char) => {
return char.charCodeAt(0)
+ ((hash << 5) - hash);
}, 0);
}
const text = "GeeksforGeeks";
console.log(createHash(text));
Output
7470298997
Approach 4: Using SHA-256 in Node.js
You can create a reusable function to generate SHA-256 hashes using the Node.js crypto module.
const crypto = require("crypto");
function createSHA256Hash(inputString) {
return crypto
.createHash("sha256")
.update(inputString)
.digest("hex");
}
const inputString = "Hello, World!";
console.log(createSHA256Hash(inputString));
Output
dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
Approach 5: Using the crypto.subtle API in the Browser
Modern browsers provide the Web Crypto API through crypto.subtle. This approach can generate cryptographic hashes without requiring external libraries.
async function hashString(inputString) {
const encoder = new TextEncoder();
const data = encoder.encode(inputString);
const hashBuffer = await crypto.subtle.digest(
"SHA-256",
data
);
const hashArray = Array.from(
new Uint8Array(hashBuffer)
);
return hashArray
.map(byte =>
byte.toString(16).padStart(2, "0")
)
.join("");
}
hashString("Hello, World!").then(hash => {
console.log(hash);
});
Output
dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
Note: Custom JavaScript hash functions are useful for non-security purposes. For passwords, authentication, or sensitive data, use appropriate security-focused password hashing algorithms and libraries rather than simple custom hashes.