Code Highlights logo
Pricing

5 Essential JavaScript Hash Methods for 2024

June 11, 2024
5 Essential JavaScript Hash Methods for 2024
Table of Contents
  • Introduction Code Example
  • Syntax and Parameters
  • crypto.subtle.digest(algorithm, data)
  • Variations
  • Example 1: Generating SHA-1 Hash
  • Example 2: Using SHA-384 for Enhanced Security
  • Example 3: Creating SHA-512 Hash for Maximum Security
  • Example 4: Hashing Passwords Securely
  • Example 5: Verifying File Integrity
  • Compatibility
  • Summary

JavaScript hashes are crucial for ensuring data integrity, secure password storage, and verifying file contents. Understanding these methods can enhance your web development skills and improve the security of your applications. In this tutorial, we'll explore five essential JavaScript hash methods you need to know in 2024.

Introduction Code Example

Let's start with a simple example of generating an SHA-256 hash in JavaScript:

1async function generateSHA256(message) {
2 const msgBuffer = new TextEncoder().encode(message);
3 const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
4 const hashArray = Array.from(new Uint8Array(hashBuffer));
5 const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
6 return hashHex;
7}
8
9generateSHA256("Hello, World!").then(hash => console.log(hash));

Syntax and Parameters

crypto.subtle.digest(algorithm, data)

  • algorithm: The name of the hash function (e.g., 'SHA-256').
  • data: The data to be hashed, typically in the form of an ArrayBuffer.

Variations

The crypto.subtle.digest method supports different algorithms like SHA-1, SHA-256, SHA-384, and SHA-512. Here's how you can use them:

1// SHA-1
2crypto.subtle.digest('SHA-1', data);
3
4// SHA-256
5crypto.subtle.digest('SHA-256', data);
6
7// SHA-384
8crypto.subtle.digest('SHA-384', data);
9
10// SHA-512
11crypto.subtle.digest('SHA-512', data);

Example 1: Generating SHA-1 Hash

1async function generateSHA1(message) {
2 const msgBuffer = new TextEncoder().encode(message);
3 const hashBuffer = await crypto.subtle.digest('SHA-1', msgBuffer);
4 const hashArray = Array.from(new Uint8Array(hashBuffer));
5 const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
6 return hashHex;
7}
8
9generateSHA1("Hello, World!").then(hash => console.log(hash));

Example 2: Using SHA-384 for Enhanced Security

1async function generateSHA384(message) {
2 const msgBuffer = new TextEncoder().encode(message);
3 const hashBuffer = await crypto.subtle.digest('SHA-384', msgBuffer);
4 const hashArray = Array.from(new Uint8Array(hashBuffer));
5 const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
6 return hashHex;
7}
8
9generateSHA384("Hello, World!").then(hash => console.log(hash));

Example 3: Creating SHA-512 Hash for Maximum Security

1async function generateSHA512(message) {
2 const msgBuffer = new TextEncoder().encode(message);
3 const hashBuffer = await crypto.subtle.digest('SHA-512', msgBuffer);
4 const hashArray = Array.from(new Uint8Array(hashBuffer));
5 const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
6 return hashHex;
7}
8
9generateSHA512("Hello, World!").then(hash => console.log(hash));

Example 4: Hashing Passwords Securely

1async function hashPassword(password) {
2 const salt = crypto.getRandomValues(new Uint8Array(16));
3 const saltedPassword = new TextEncoder().encode(password + salt);
4 const hashBuffer = await crypto.subtle.digest('SHA-256', saltedPassword);
5 const hashArray = Array.from(new Uint8Array(hashBuffer));
6 const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
7 return { salt, hashHex };
8}
9
10hashPassword("mySecurePassword").then(result => {
11 console.log(`Salt: ${result.salt}`);
12 console.log(`Hash: ${result.hashHex}`);
13});

Example 5: Verifying File Integrity

1async function verifyFileIntegrity(file) {
2 const arrayBuffer = await file.arrayBuffer();
3 const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
4 const hashArray = Array.from(new Uint8Array(hashBuffer));
5 const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
6 return hashHex;
7}
8
9// Usage with a file input element
10document.getElementById('fileInput').addEventListener('change', async (event) => {
11 const file = event.target.files[0];
12 const fileHash = await verifyFileIntegrity(file);
13 console.log(`File Hash: ${fileHash}`);
14});

Compatibility

The crypto.subtle.digest method is supported in modern browsers like Chrome, Firefox, Edge, and Safari. However, it's always good practice to check compatibility on MDN Web Docs.

Summary

In this tutorial, we've covered five essential JavaScript hash methods, including SHA-1, SHA-256, SHA-384, and SHA-512. We also explored practical examples such as hashing passwords and verifying file integrity. Understanding these methods can significantly enhance your web development skills and ensure the security of your applications.

For further learning, consider exploring our JavaScript course and Introduction to Web Development. Additionally, you can learn more about HTML and CSS through our HTML Fundamentals Course and CSS Introduction.

For more detailed information on cryptographic functions in JavaScript, you may refer to MDN Web Docs and Web Crypto API.

Related courses

1 Course

Javascript Fundamentals Course

Javascript Fundamentals

4.7+
834 reviews

Stay Ahead with Code highlights

Join our community of forward-thinkers and innovators. Subscribe to get the latest updates on courses, exclusive insights, and tips from industry experts directly to your inbox.

3D Letter

Related articles

9 Articles

Start learning for free

If you've made it this far, you must be at least a little curious. Sign up and grow your programming skills with Code Highlights.

Start learning for free like this happy man with Code Highlights