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.


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


  • 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


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)

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

sha256("foobar").then(function(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 = => ('00' + b.toString(16)).slice(-2)).join('');
    return hashHex;

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

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


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

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support37123411248
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support5353 ?3437 ?6.0

See also

Document Tags and Contributors

Last updated by: gregtatum,