Introduction to js-sha3
The js-sha3 library is a powerful tool for applying cryptographic hash functions in your JavaScript applications. It supports various SHA-3 hashing algorithms and provides a simple API for generating secure hashes.
API Usage and Examples
Basic Hashing
Generate a SHA-3 hash of a simple string.
const sha3 = require('js-sha3').sha3_256; console.log(sha3('hello')); // Outputs: 6dcd4ce23d88e2ee4453479261b82bd4d4e4486e9c7fae90fbd06327606ded7b
Hashing with Different SHA-3 Variants
js-sha3 supports different SHA-3 variants. Here are examples for SHA-384 and SHA-512.
const sha3_384 = require('js-sha3').sha3_384; const sha3_512 = require('js-sha3').sha3_512;
console.log(sha3_384('hello')); // Outputs: b7e23ec29af22b0b4e41da31e868d57226121c84abc163a5c11fb2d9e6a299e1cb7b27269f053ff00c4f9466f773ed12 console.log(sha3_512('hello')); // Outputs: 3946e29e4e462baacef0ba3c9fe1240d0c4d4dc1cf5482cf3e253904ab1467b7aae03bb12ab6ce9f4ba48a5d80a6e38bc
Creating an HMAC
Generate a Hash-based Message Authentication Code (HMAC) using SHA-3.
const { sha3_256, hmac } = require('js-sha3'); const hmacSha3 = hmac.sha3_256;
console.log(hmacSha3('key', 'message')); // Outputs: 93dd392a4cfb53b7a7f1ae5d6bbd0bed5d96a7c5c6dd808d1e4b5e49ae3e31e8
Example: Building a Node.js Application
Let’s create a simple Node.js application that uses js-sha3 for password hashing.
const express = require('express'); const { sha3_256 } = require('js-sha3'); const bodyParser = require('body-parser');
const app = express(); app.use(bodyParser.json());
// Endpoint to hash a password app.post('/hash-password', (req, res) => {
const { password } = req.body;
const hash = sha3_256(password);
res.json({ hash });
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
This application provides an endpoint /hash-password
that takes a password as input and returns the hashed value using SHA-3.
Conclusion: The js-sha3 library is a versatile and efficient solution for integrating SHA-3 hashing into your JavaScript applications. Whether you are generating simple hashes, HMACs, or building more complex hashing mechanisms, js-sha3 offers a robust API to meet your cryptographic needs.
Hash: 014360d86c0d1ad57b59121ccbec90afde2f2c2327a38760c2546d146d249feb