What is rev-hash?
The rev-hash npm package is used to generate a hash from a file or string content. This is particularly useful for cache busting in web development, where you need to ensure that users get the latest version of a file.
What are rev-hash's main functionalities?
Generate hash from file content
This feature allows you to generate a hash from the content of a file. You read the file into a buffer and then pass that buffer to the rev-hash function to get the hash.
const revHash = require('rev-hash');
const fs = require('fs');
const buffer = fs.readFileSync('path/to/file');
const hash = revHash(buffer);
console.log(hash);
Generate hash from string content
This feature allows you to generate a hash from a string. You simply pass the string to the rev-hash function to get the hash.
const revHash = require('rev-hash');
const content = 'Hello, world!';
const hash = revHash(content);
console.log(hash);
Other packages similar to rev-hash
hasha
The hasha package is a versatile hashing library that supports multiple hashing algorithms like MD5, SHA-1, SHA-256, etc. It can hash strings, buffers, and even streams. Compared to rev-hash, hasha offers more flexibility in terms of hashing algorithms and input types.
crypto
The crypto package is a built-in Node.js module that provides cryptographic functionality, including a wide range of hash algorithms. It is more complex and powerful than rev-hash, offering more control over the hashing process but requiring more setup.
md5-file
The md5-file package is specifically designed to generate MD5 hashes from files. It is similar to rev-hash in that it focuses on file content hashing, but it is limited to the MD5 algorithm.
rev-hash
Create a hash for file revving
It will create an md5
hash from an input buffer or string, and truncate it to 10 characters, which is unique enough for this purpose.
If you think you need a different hash algorithm or a longer hash, you're wrong.
Install
npm install rev-hash
Usage
import fs from 'node:fs';
import revisionHash from 'rev-hash';
revisionHash(fs.readFileSync('unicorn.png'));
revisionHash('Lorem ipsum dolor sit amet');
API
revisionHash(input)
input
Type: Buffer | string
The data to create a hash from.