Node crypto.hash function | API Reference | Bun
BuildDocsReferenceGuidesBlogDiscord/
node:crypto/
hashFhash
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
node:crypto/
hashFhash
function
crypto.hash
console.log(crypto.hash('sha1', Buffer.from(base64, 'base64'), 'buffer'));
```" data-algolia-static="false" data-algolia-merged="false" data-type="Function">function
hash(algorithm: string,data:
BinaryLike,options?:
BinaryToTextEncoding |
OneShotDigestOptionsWithStringEncoding): string;
A utility for creating one-shot hash digests of data. It can be faster than the object-based crypto.createHash() when hashing a smaller amount of data (crypto.createHash() instead.
The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are 'sha256', 'sha512', etc. On recent releases of OpenSSL, openssl list -digest-algorithms will display the available digest algorithms.
If options is a string, then it specifies the outputEncoding.
Example:
import crypto from 'node:crypto';
import { Buffer } from 'node:buffer';
// Hashing a string and return the result as a hex-encoded string.
const string = 'Node.js';
// 10b3493287f831e81a438811a1ffba01f8cec4b7
console.log(crypto.hash('sha1', string));
// Encode a base64-encoded string into a Buffer, hash it and return
// the result as a buffer.
const base64 = 'Tm9kZS5qcw==';
//
console.log(crypto.hash('sha1', Buffer.from(base64, 'base64'), 'buffer'));
@param data
When data is a string, it will be encoded as UTF-8 before being hashed. If a different input encoding is desired for a string input, user could encode the string into a TypedArray using either TextEncoder or Buffer.from() and passing the encoded TypedArray into this API instead.
function
hash(algorithm: string,data:
BinaryLike,options: 'buffer' |
OneShotDigestOptionsWithBufferEncoding): NonSharedBuffer;
A utility for creating one-shot hash digests of data. It can be faster than the object-based crypto.createHash() when hashing a smaller amount of data (crypto.createHash() instead.
The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are 'sha256', 'sha512', etc. On recent releases of OpenSSL, openssl list -digest-algorithms will display the available digest algorithms.
If options is a string, then it specifies the outputEncoding.
Example:
import crypto from 'node:crypto';
import { Buffer } from 'node:buffer';
// Hashing a string and return the result as a hex-encoded string.
const string = 'Node.js';
// 10b3493287f831e81a438811a1ffba01f8cec4b7
console.log(crypto.hash('sha1', string));
// Encode a base64-encoded string into a Buffer, hash it and return
// the result as a buffer.
const base64 = 'Tm9kZS5qcw==';
//
console.log(crypto.hash('sha1', Buffer.from(base64, 'base64'), 'buffer'));
@param data
When data is a string, it will be encoded as UTF-8 before being hashed. If a different input encoding is desired for a string input, user could encode the string into a TypedArray using either TextEncoder or Buffer.from() and passing the encoded TypedArray into this API instead.
function
hash(algorithm: string,data:
BinaryLike,options: 'buffer' |
BinaryToTextEncoding |
OneShotDigestOptions): string | NonSharedBuffer;
A utility for creating one-shot hash digests of data. It can be faster than the object-based crypto.createHash() when hashing a smaller amount of data (crypto.createHash() instead.
The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are 'sha256', 'sha512', etc. On recent releases of OpenSSL, openssl list -digest-algorithms will display the available digest algorithms.
If options is a string, then it specifies the outputEncoding.
Example:
import crypto from 'node:crypto';
import { Buffer } from 'node:buffer';
// Hashing a string and return the result as a hex-encoded string.
const string = 'Node.js';
// 10b3493287f831e81a438811a1ffba01f8cec4b7
console.log(crypto.hash('sha1', string));
// Encode a base64-encoded string into a Buffer, hash it and return
// the result as a buffer.
const base64 = 'Tm9kZS5qcw==';
//
console.log(crypto.hash('sha1', Buffer.from(base64, 'base64'), 'buffer'));
@param data
When data is a string, it will be encoded as UTF-8 before being hashed. If a different input encoding is desired for a string input, user could encode the string into a TypedArray using either TextEncoder or Buffer.from() and passing the encoded TypedArray into this API instead.
Referenced typestype
BinaryLike = string | NodeJS.ArrayBufferViewtype
BinaryToTextEncoding = 'base64' | 'base64url' | 'hex' | 'binary'interface
OneShotDigestOptionsWithStringEncodingoutputEncoding?:
BinaryToTextEncodingEncoding used to encode the returned digest.
outputLength?: number
For XOF hash functions such as 'shake256', the outputLength option can be used to specify the desired output length in bytes.
interface
OneShotDigestOptionsWithBufferEncodingoutputEncoding: 'buffer'
Encoding used to encode the returned digest.
outputLength?: number
For XOF hash functions such as 'shake256', the outputLength option can be used to specify the desired output length in bytes.
interface
OneShotDigestOptionsoutputEncoding?: 'buffer' |
BinaryToTextEncodingEncoding used to encode the returned digest.
outputLength?: number
For XOF hash functions such as 'shake256', the outputLength option can be used to specify the desired output length in bytes.
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →