W3cubDocs

/DOM

SubtleCrypto.digest

The SubtleCrypto.digest() method returns a Promise of a digest generated from the hash function and text given as parameters.

Warning: Older insecure hash functions, like MD5, are not supported by this method. Even a supported method, SHA-1, is considered weak, has been broken and should be avoided for cryptographic applications.

Syntax

var hash = crypto.subtle.digest(algo, buffer);

Parameters

  • algo is a DOMString defining the hash function to use. Supported values are: SHA-1, SHA-256, SHA-384, and SHA-512.
  • buffer is an ArrayBuffer or an ArrayBufferView containing the data to be hashed using the hashing algorithm.

Return value

  • hash is a Promise that returns the hash on success.

Example

Here's an example that computes the sha256 of a string and display its hex digest.

function sha256(str) {
  // We transform the string into an arraybuffer.
  var buffer = new TextEncoder("utf-8").encode(str);
  return crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
    return hex(hash);
  });
}

function hex(buffer) {
  var hexCodes = [];
  var view = new DataView(buffer);
  for (var i = 0; i < view.byteLength; i += 4) {
    // Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
    var value = view.getUint32(i)
    // toString(16) will give the hex representation of the number without padding
    var stringValue = value.toString(16)
    // We use concatenation and slice for padding
    var padding = '00000000'
    var paddedValue = (padding + stringValue).slice(-padding.length)
    hexCodes.push(paddedValue);
  }

  // Join all the hex strings into one
  return hexCodes.join("");
}

sha256("foobar").then(function(digest) {
  console.log(digest);
}); // outputs "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"

And an example using async/await.

async function sha256(message) {

    // encode as UTF-8
    const msgBuffer = new TextEncoder('utf-8').encode(message);

    // hash the message
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
    
    // convert ArrayBuffer to Array
    const hashArray = Array.from(new Uint8Array(hashBuffer));

    // convert bytes to hex string
    const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
    return hashHex;
}

sha256('abc').then(hash => console.log(hash));

(async function() {
    const hash = await sha256('abc');
}());

Specifications

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

Browser compatibility

Feature Chrome Firefox (Gecko) Edge Internet Explorer Opera Safari
Basic support (SHA-1 and SHA-2 algorithms) 37 34 (34) (Yes) No support ? No support
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (SHA-1 and SHA-2 algorithms) (Yes) 37 (Yes) 34.0 (34) No support ? No support

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest