WebCrypto
构造函数
new WebCrypto()
new WebCrypto(): WebCrypto
返回值
属性
subtle
staticsubtle:typeof (Anonymous class)= `class { static async importKey(format: KeyFormat, data: ArrayBuffer | Uint8Array | DataView, algorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: Array) : Promise { // 仅实现了'raw'格式。return new Promise ((resolve, reject) => { let key : ArrayBuffer; let keyData : ArrayBuffer; // 根据WebCrypto规范深拷贝Key // 参考: (第2.2点)// https://www.w3.org/TR/WebCryptoAPI/#dfn-SubtleCrypto-method-importKey(仅提供英文版) if (data instanceof ArrayBuffer) { keyData = new Uint8Array(data).buffer; key = data; } else if(data instanceof Uint8Array || data instanceof DataView ) { keyData = new Uint8Array(data.buffer).buffer; key = data.buffer; } else { LogUtil.error("WebCrypto::importKey(): data type error"); reject(); } let keyObj: CryptoKeyObject = W3CMediaTurboModule.importKey(format, keyData, algorithm, extractable, keyUsages); // 目前,尚无原生等效方法可用于 // 在JSONObject中添加Array,因此在此处分配key和usages。if (extractable) { keyObj.key = key; } keyObj.usages = keyUsages; keyObj.algorithm = algorithm; let result: CryptoKey = keyObj; if (result.usages.length >= 1) { resolve(result); return; } reject(); }); }
static async decrypt(algorithm: AlgorithmIdentifier, key: CryptoKey,
data: BufferSource) : Promise<any> {
return new Promise<ArrayBuffer>((resolve, reject) => {
if (algorithm === undefined || key === undefined ||
(algorithm.name !== "AES-CTR" && algorithm.iv === undefined && key.algorithm.iv === undefined) ||
(algorithm.name === "AES-CTR" && algorithm.counter === undefined && key.algorithm.counter === undefined) ||
data === undefined) {
LogUtil.error("WebCrypto::decrypt invalid data passed");
reject();
}
let ivCounterArray : ArrayBuffer;
if (algorithm.name === "AES-CTR") {
if (algorithm.counter instanceof ArrayBuffer) {
ivCounterArray = algorithm.counter;
} else {
if (algorithm.counter.byteOffset == 0 && algorithm.counter.byteLength == algorithm.counter.buffer.byteLength) {
// 这是覆盖整个缓冲区的TypedArray。
ivCounterArray = algorithm.counter.buffer;
} else {
// 这是缓冲区上的一个“视图”。 创建一个仅包含数据的
// 新缓冲区。 请注意,由于这不是ArrayBuffer,因此“new”调用
// 将分配一个新的缓冲区来保存副本。
ivCounterArray = new Uint8Array(algorithm.counter.buffer).buffer;
}
}
}
else {
if (algorithm.iv instanceof ArrayBuffer) {
ivCounterArray = algorithm.iv;
} else {
if (algorithm.iv.byteOffset == 0 && algorithm.iv.byteLength == algorithm.iv.buffer.byteLength) {
// 这是覆盖整个缓冲区的TypedArray。
ivCounterArray = algorithm.iv.buffer;
} else {
// 这是缓冲区上的一个“视图”。 创建一个仅包含数据的
// 新缓冲区。 请注意,由于这不是ArrayBuffer,因此“new”调用
// 将分配一个新的缓冲区来保存副本。
ivCounterArray = new Uint8Array(algorithm.iv.buffer).buffer;
}
}
}
// 将ivArray复制到CryptoKey
key.algorithm.iv = ivCounterArray;
let plainData: ArrayBuffer = W3CMediaTurboModule.decrypt(key.algorithm, key, ivCounterArray, data);
if (plainData.byteLength >= 1) {
resolve(plainData);
return;
}
LogUtil.error("WebCrypto::decrypt Errored out");
reject();
});
}
}`
方法
randomUUID()
staticrandomUUID():string
返回值
字符串
Last updated: 2026年7月22日

