安全上下文
此特征只可用于 安全上下文 (HTTPS),在某些或所有 支持浏览器 .

digest() 方法在 SubtleCrypto interface generates a digest of the given data. 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 a Promise which will be fulfilled with the digest.

句法

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

参数

  • algorithm 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 ArrayBuffer or ArrayBufferView containing the data to be digested.

返回值

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, and produces an output 160 bits long.

警告 : This algorithm is now considered vulnerable and should not be used for cryptographic applications.

SHA-256

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

SHA-384

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

SHA-512

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

Hint: If you are looking here for how to create an keyed-hash message authentication code ( HMAC ), you need to use the SubtleCrypto.sign() 代替。

范例

基本范例

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

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 crypto.subtle.digest('SHA-256', data);
  return hash;
}
const digestBuffer = await digestMessage(text);
console.log(digestBuffer.byteLength);
					

Converting a digest to a hex string

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

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 crypto.subtle.digest('SHA-256', msgUint8);           // hash the message
  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;
}
const digestHex = await digestMessage(text);
console.log(digestHex);
					

规范

规范 状态 注释
Web Cryptography API
The definition of 'SubtleCrypto.digest()' in that specification.
推荐 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
digest Chrome 37 Edge 部分支持 12
部分支持 12
Not supported: SHA-1.
Firefox 34
34
不支持 32 — 34 Disabled
Disabled ). To change preferences in Firefox, visit
IE 部分支持 11
部分支持 11
返回 CryptoOperation 而不是 Promise
Opera 24 Safari 7 WebView Android 37 Chrome Android 37 Firefox Android 34
34
不支持 32 — 34 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 24 Safari iOS 7 Samsung Internet Android 6.0

图例

完整支持

完整支持

部分支持

部分支持

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

In Chrome 60, they added a feature that disables crypto.subtle for non-TLS connections.

另请参阅

元数据

  • 最后修改:
  1. Web 加密 API
  2. SubtleCrypto
  3. 方法
    1. decrypt()
    2. deriveBits()
    3. deriveKey()
    4. digest()
    5. encrypt()
    6. exportKey()
    7. generateKey()
    8. importKey()
    9. sign()
    10. unwrapKey()
    11. verify()
    12. wrapKey()
  4. Related pages for Web Crypto API
    1. Crypto
    2. CryptoKey
    3. CryptoKeyPair
    4. RandomSource
    5. Window.crypto

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1