Crypto
object cannot be constructed directly. It is globally injected during the runtime of edge functions. Directly use the global crypto instance.// Perform encoding.const encodeContent = new TextEncoder().encode('hello world');// Use crypto to perform SHA-256 hashing. The resulting hash is a promise that fulfills with an ArrayBuffer.const sha256Content = await crypto.subtle.digest({ name: 'SHA-256' },encodeContent);const result = new Uint8Array(sha256Content);
// crypto.subtlereadonly subtle: SubtleCrypto;
crypto.getRandomValues(buffer: TypedArray): TypedArray;
Parameter | Type | Required | Description |
buffer | Yes | The buffer of random values. The value cannot exceed 65,536 bytes in length. For more information, see TypedArray. |
crypto.randomUUID(): string;
encrypt/decrypt
, sign/verify
, and digest
. Such methods can be used to implement security-related features such as privacy and identity verification.generateKey
, deriveKey
, and importKey/exportKey
. Such methods can be used to manage keys.crypto.subtle.digest(algorithm: string | object, data: ArrayBuffer): Promise<ArrayBuffer>;
crypto.subtle.encrypt(algorithm: object, key: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer>;
RSA-OAEP
algorithm, the data length must not exceed modulusLength/8 - (2 * hLen) - 2, where hLen is determined as follows:AES-CTR
, AES-CBC
++++, and AES-GCM
algorithms, the data length is limited to 1 MB.crypto.subtle.decrypt(algorithm: object, key: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer>;
RSA-OAEP
algorithm, the data length is modulusLength/8.AES-CTR
, AES-CBC
++++, and AES-GCM
algorithms, the data length is limited to 1 MB.crypto.subtle.sign(algorithm: string | object, key: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer>;
crypto.subtle.verify(algorithm: string | object, key: CryptoKey, signature: BufferSource, data: ArrayBuffer): Promise<boolean>;
crypto.subtle.generateKey(algorithm: object, extractable: boolean, keyUsages: Array<string>): Promise<CryptoKey | CryptoKeyPair>;
crypto.subtle.deriveKey(algorithm: object, baseKey: CryptoKey, derivedKeyAlgorithm: object, extractable: boolean, keyUsages: Array<string>): Promise<CryptoKey>;
crypto.subtle.importKey(format: string, keyData: BufferSource, algorithm: string | object, extractable: boolean, keyUsages: Array<string>): Promise<CryptoKey>;
crypto.subtle.exportKey(format: string, key: CryptoKey): Promise<ArrayBuffer>;
crypto.subtle.deriveBits(algorithm: object, baseKey: CryptoKey, length: integer): Promise<ArrayBuffer>;
crypto.subtle.wrapKey(format: string, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgo: string | object): Promise<ArrayBuffer>;;
crypto.subtle.unwrapKey(format: string, wrappedKey: ArrayBuffer, unwrappingKey: CryptoKey, unwrapAlgo: string | object, unwrappedKeyAlgo: string | object, extractable: boolean, keyUsages: Array<string>): Promise<CryptoKey>;
CryptoKey
represents a key that is generated by using an encryption algorithm. For more information, see CryptoKey. The CryptoKey object cannot be constructed directly. You can use the following methods to generate CryptoKey objects:CryptoKey
attributes.Attribute | Type | Read-only | Description |
type | string | Yes | The type of the key. |
extractable | boolean | Yes | Specifies whether the key can be exported. |
algorithm | object | Yes | The algorithm for which this key can be used and the associated parameters. |
usages | Array<string> | Yes | The usage of the key. |
CryptoKeyPair
represents a key pair that is generated by using an encryption algorithm. For more information, see CryptoKeyPair. The CryptoKeyPair object cannot be constructed directly. You can use the following method to generate CryptoKeyPair objects:CryptoKeyPair
attributes.Attribute | Type | Read-only | Description |
privateKey | Yes | The private key. For encryption and decryption algorithms, this key is used for decryption. For signature signing and verification algorithms, this key is used for signature signing. | |
publicKey | Yes | The public key. For encryption and decryption algorithms, this key is used for encryption. For signature signing and verification algorithms, this key is used for signature verification. |
Algorithm | encrypt() decrypt() | sign() verify() | wrapKey() unwrapKey() | deriveKey() deriveBits() | generateKey() | importKey() | exportKey() | digest() |
RSASSA-PKCS1-v1_5 | - | ✓ | - | - | ✓ | ✓ | ✓ | - |
RSA-PSS | - | ✓ | - | - | ✓ | ✓ | ✓ | - |
RSA-OAEP | ✓ | - | ✓ | - | ✓ | ✓ | ✓ | - |
ECDSA | - | ✓ | - | - | ✓ | ✓ | ✓ | - |
ECDH | - | - | - | ✓ | ✓ | ✓ | ✓ | - |
HMAC | - | ✓ | - | - | ✓ | ✓ | ✓ | - |
AES-CTR | ✓ | - | ✓ | - | ✓ | ✓ | ✓ | - |
AES-CBC | ✓ | - | ✓ | - | ✓ | ✓ | ✓ | - |
AES-GCM | ✓ | - | ✓ | - | ✓ | ✓ | ✓ | - |
AES-KW | - | - | ✓ | - | ✓ | ✓ | ✓ | - |
HKDF | - | - | - | ✓ | - | ✓ | - | - |
PBKDF2 | - | - | - | ✓ | - | ✓ | - | - |
SHA-1 | - | - | - | - | - | - | - | ✓ |
SHA-256 | - | - | - | - | - | - | - | ✓ |
SHA-384 | - | - | - | - | - | - | - | ✓ |
SHA-512 | - | - | - | - | - | - | - | ✓ |
MD5 | - | - | - | - | - | - | - | ✓ |
function uint8ArrayToHex(arr) {return Array.prototype.map.call(arr, (x) => ((`0${x.toString(16)}`).slice(-2))).join('');}async function handleEvent(event) {const encodeArr = TextEncoder().encode('hello world');// Execute MD5.const md5Buffer = await crypto.subtle.digest({ name: 'MD5' }, encodeArr);// Generate a hexadecimal string.const md5Str = uint8ArrayToHex(new Uint8Array(md5Buffer));const response = new Response(md5Str);return response;}addEventListener('fetch', async (event) => {event.respondWith(handleEvent(event));});
Was this page helpful?