Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. SubtleCrypto
  4. digest()

SubtleCrypto: digest() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨January 2020⁩.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

Note: This feature is available inWeb Workers.

Thedigest() method of theSubtleCrypto interface generates adigest of the given data, using the specifiedhash function.A digest is a short fixed-length value derived from some variable-length input.Cryptographic digests should exhibit collision-resistance, meaning that it's hard to come up with two different inputs that have the same digest value.

It takes as its arguments an identifier for the digest algorithm to use and the data to digest.It returns aPromise which will be fulfilled with the digest.

Note that this API does not support streaming input: you must read the entire input into memory before passing it into the digest function.

Syntax

js
digest(algorithm, data)

Parameters

algorithm

This may be a string or an object with a single propertyname that is a string. The string names 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

AnArrayBuffer, aTypedArray or aDataView object containing the data to be digested.

Return value

APromise that fulfills with anArrayBuffer containing the digest.

Supported algorithms

Digest algorithms, also known ashash 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.

AlgorithmOutput length (bits)Block size (bits)Specification
SHA-1160512FIPS 180-4, section 6.1
SHA-256256512FIPS 180-4, section 6.2
SHA-3843841024FIPS 180-4, section 6.5
SHA-5125121024FIPS 180-4, section 6.4

Warning:SHA-1 is now considered vulnerable and should not be used for cryptographic applications.

Note:If you are looking here for how to create a keyed-hash message authentication code (HMAC), you need to use theSubtleCrypto.sign() instead.

Examples

For more examples of using thedigest() API, seeNon-cryptographic uses of SubtleCrypto.

Basic example

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

js
const text =  "An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.";async function digestMessage(message) {  const encoder = new TextEncoder();  const data = encoder.encode(message);  const hash = await window.crypto.subtle.digest("SHA-256", data);  return hash;}digestMessage(text).then((digestBuffer) =>  console.log(digestBuffer.byteLength),);

Converting a digest to a hex string

The digest is returned as anArrayBuffer, but for comparison and display digests are often represented as hex strings.This example calculates a digest, then converts theArrayBuffer to a hex string:

js
const text =  "An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.";async function digestMessage(message) {  const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array  const hashBuffer = await window.crypto.subtle.digest("SHA-256", msgUint8); // hash the message  const hashHex = new Uint8Array(hashBuffer).toHex(); // Convert ArrayBuffer to hex string.  return hashHex;}digestMessage(text).then((digestHex) => console.log(digestHex));

The above example usesUint8Array.fromHex(), which became available in 2025.To support older browsers, the following alternative can be used instead:

js
const text =  "An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.";async function digestMessage(message) {  const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array  const hashBuffer = await window.crypto.subtle.digest("SHA-256", msgUint8); // hash the message  if (Uint8Array.prototype.toHex) {    // Use toHex if supported.    return new Uint8Array(hashBuffer).toHex(); // Convert ArrayBuffer to hex string.  }  // If toHex() is not supported, fall back to an alternative implementation.  const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array  const hashHex = hashArray    .map((b) => b.toString(16).padStart(2, "0"))    .join(""); // convert bytes to hex string  return hashHex;}digestMessage(text).then((digestHex) => console.log(digestHex));

Specifications

Specification
Web Cryptography Level 2
# SubtleCrypto-method-digest

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp