SubtleCrypto.digest()

The SubtleCrypto.digest() method generates a digest of the given data.

It takes as arguments an identifier for the digest algorithm to use and the data to digest. It returns a Promise which will be resolved with the digest.

Syntax

var digest = crypto.subtle.digest(algorithm, data);

Parameters

  • algorithm is a DOMString defining the hash function to use. Supported values are:
    • SHA-1 (but don't use this in cryptographic applications)
    • SHA-256
    • SHA-384
    • SHA-512.
  • data is an ArrayBuffer or an ArrayBufferView containing the data to be digested.

Return value

Supported algorithms

Digest algorithms, also known as cryptographic hash functions, transform an arbitrarily large block of data into a fixed-size output, usually much shorter than the input. They have a variety of applications in cryptography.

SHA-1

This algorithm is specified in FIPS 180-4, section 6.1. Produces an output 160 bits long.

Note that this algorithm is now considered vulnerable and should not be used.

SHA-256

This algorithm is specified in FIPS 180-4, section 6.2. Produces an output 256 bits long.

SHA-384

This algorithm is specified in FIPS 180-4, section 6.5. Produces an output 384 bits long.

SHA-512

This algorithm is specified in FIPS 180-4, section 6.4. Produces an output 512 bits long.

Example

This example encodes a message, then calculates its SHA-256 digest and logs the digest length:

let text = 'An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.';

async function digestMessage(message) {
  let encoder = new TextEncoder();
  let data = encoder.encode(message);
  let digest = await window.crypto.subtle.digest('SHA-256', data);
  console.log(digest.byteLength); // 32 for SHA-256
}

digestMessage(text);

Specifications

Specification Status Comment
Web Cryptography API
The definition of 'SubtleCrypto.digest()' in that specification.
Recommendation Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 37Edge Partial support 12
Notes
Partial support 12
Notes
Notes Not supported: SHA-1.
Firefox Full support 34
Full support 34
No support 32 — 34
Disabled
Disabled From version 32 until version 34 (exclusive): this feature is behind the dom.webcrypto.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Partial support 11
Notes
Partial support 11
Notes
Notes Returns CryptoOperation instead of Promise
Opera Full support 24Safari Full support 7WebView Android Full support 37Chrome Android Full support 37Edge Mobile Full support 12Firefox Android Full support 34
Full support 34
No support 32 — 34
Disabled
Disabled From version 32 until version 34 (exclusive): this feature is behind the dom.webcrypto.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 24Safari iOS Full support 7Samsung Internet Android Full support 6.0

Legend

Full support  
Full support
Partial support  
Partial support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also

Document Tags and Contributors

Last updated by: wbamberg,