Web Crypto API
的性能更高。Crypto
对象,边缘函数运行时会在全局注入,直接使用全局 crypto 实例即可。// 编码const encodeContent = new TextEncoder().encode('hello world');// 使用 crypto,生成 SHA-256 哈希值 Promise<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;
属性名 | 类型 | 必填 | 说明 |
buffer | 是 |
crypto.randomUUID(): string;
encrypt/decrypt
、sign/verify
、digest
, 可以用来实现隐私和身份验证等安全功能。generateKey
、deriveKey
、importKey/exportKey
, 可以用来管理密钥。crypto.subtle.digest(algorithm: string | object, data: ArrayBuffer): Promise<ArrayBuffer>;
crypto.subtle.encrypt(algorithm: object, key: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer>;
RSA-OAEP
算法,要求 data 长度不能超过 modulusLength/8 - 2*hLen -2,其中 hLen 的取值逻辑为:AES-CTR
,AES-CBC
,AES-GCM
,限制 data 长度 1MB。crypto.subtle.decrypt(algorithm: object, key: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer>;
RSA-OAEP
算法, data 长度为 modulusLength/8。AES-CTR
,AES-CBC
,AES-GCM
,限制 data 长度 1MB。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
属性描述如下。属性名 | 类型 | 只读 | 说明 |
type | string | 是 | 密钥类型。 |
extractable | boolean | 是 | 密钥是否可导出。 |
algorithm | object | 是 | 算法相关, 包含算法需要的字段。 |
usages | Array<string> | 是 | 密钥的用途。 |
CryptoKeyPair
属性描述如下。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');// 执行 md5const md5Buffer = await crypto.subtle.digest({ name: 'MD5' }, encodeArr);// 输出十六进制字符串const md5Str = uint8ArrayToHex(new Uint8Array(md5Buffer));const response = new Response(md5Str);return response;}addEventListener('fetch', async (event) => {event.respondWith(handleEvent(event));});
本页内容是否解决了您的问题?