WebCrypto
Constructors
new WebCrypto()
new WebCrypto(): WebCrypto
Returns
Properties
subtle
staticsubtle:typeof (Anonymous class)= `class { static async importKey(format: KeyFormat, data: ArrayBuffer | Uint8Array | DataView, algorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: Array) : Promise { // Only 'raw' format is implemented. return new Promise ((resolve, reject) => { let key : ArrayBuffer; let keyData : ArrayBuffer; // take deep copy of the Key as per WebCrypto spec // Refer: (Point 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); // At present, there is no native equivalent of // adding Array in JSONObject, so assigning key and usages here. 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) {
// This is a TypedArray over the whole buffer.
ivCounterArray = algorithm.counter.buffer;
} else {
// This is a "view" on the buffer. Create a new buffer that only contains
// the data. Note that since this isn't an ArrayBuffer, the "new" call
// will allocate a new buffer to hold the copy.
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) {
// This is a TypedArray over the whole buffer.
ivCounterArray = algorithm.iv.buffer;
} else {
// This is a "view" on the buffer. Create a new buffer that only contains
// the data. Note that since this isn't an ArrayBuffer, the "new" call
// will allocate a new buffer to hold the copy.
ivCounterArray = new Uint8Array(algorithm.iv.buffer).buffer;
}
}
}
// Copy ivArray to 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();
});
}
}`
Methods
randomUUID()
staticrandomUUID():string
Returns
string
Last updated: Jul 22, 2026

