'use strict';
(({
cryptoGetRandomValues,
cryptoRandomUUID,
cryptoSubtleDigest,
cryptoSubtleSign,
cryptoSubtleVerify,
cryptoSubtleEncrypt,
cryptoSubtleDecrypt,
cryptoSubtleGenerateKey,
cryptoSubtleImportKey,
cryptoSubtleExportKey,
}) => {
const subtle = {
async digest(algorithm, data) {
let alg;
if (typeof algorithm === 'string') {
alg = algorithm;
} else if (algorithm && typeof algorithm === 'object' && algorithm.name) {
alg = algorithm.name;
} else {
throw new TypeError('Invalid algorithm');
}
if (data === null || data === undefined) {
throw new TypeError('data is required');
}
return cryptoSubtleDigest(alg, data);
},
async sign(algorithm, key, data) {
if (!algorithm) {
throw new TypeError('algorithm is required');
}
if (!key) {
throw new TypeError('key is required');
}
if (data === null || data === undefined) {
throw new TypeError('data is required');
}
return cryptoSubtleSign(algorithm, key, data);
},
async verify(algorithm, key, signature, data) {
if (!algorithm) {
throw new TypeError('algorithm is required');
}
if (!key) {
throw new TypeError('key is required');
}
if (signature === null || signature === undefined) {
throw new TypeError('signature is required');
}
if (data === null || data === undefined) {
throw new TypeError('data is required');
}
return cryptoSubtleVerify(algorithm, key, signature, data);
},
async encrypt(algorithm, key, data) {
if (!algorithm) {
throw new TypeError('algorithm is required');
}
if (!key) {
throw new TypeError('key is required');
}
if (data === null || data === undefined) {
throw new TypeError('data is required');
}
return cryptoSubtleEncrypt(algorithm, key, data);
},
async decrypt(algorithm, key, data) {
if (!algorithm) {
throw new TypeError('algorithm is required');
}
if (!key) {
throw new TypeError('key is required');
}
if (data === null || data === undefined) {
throw new TypeError('data is required');
}
return cryptoSubtleDecrypt(algorithm, key, data);
},
async generateKey(algorithm, extractable, keyUsages) {
if (!algorithm) {
throw new TypeError('algorithm is required');
}
if (typeof extractable !== 'boolean') {
throw new TypeError('extractable must be a boolean');
}
if (!Array.isArray(keyUsages)) {
throw new TypeError('keyUsages must be an array');
}
return cryptoSubtleGenerateKey(algorithm, extractable, keyUsages);
},
async importKey(format, keyData, algorithm, extractable, keyUsages) {
if (!format) {
throw new TypeError('format is required');
}
if (keyData === null || keyData === undefined) {
throw new TypeError('keyData is required');
}
if (!algorithm) {
throw new TypeError('algorithm is required');
}
if (typeof extractable !== 'boolean') {
throw new TypeError('extractable must be a boolean');
}
if (!Array.isArray(keyUsages)) {
throw new TypeError('keyUsages must be an array');
}
return cryptoSubtleImportKey(format, keyData, algorithm, extractable, keyUsages);
},
async exportKey(format, key) {
if (!format) {
throw new TypeError('format is required');
}
if (!key) {
throw new TypeError('key is required');
}
return cryptoSubtleExportKey(format, key);
},
};
const crypto = {
getRandomValues(typedArray) {
if (typedArray === null || typedArray === undefined) {
throw new TypeError('getRandomValues: argument 1 is required');
}
return cryptoGetRandomValues(typedArray);
},
randomUUID() {
return cryptoRandomUUID();
},
get subtle() {
return subtle;
},
};
Object.defineProperty(globalThis, 'crypto', {
value: crypto,
writable: false,
enumerable: true,
configurable: false,
});
Object.defineProperty(globalThis, 'SubtleCrypto', {
value: function SubtleCrypto() {
throw new TypeError('Illegal constructor');
},
writable: true,
enumerable: false,
configurable: true,
});
Object.defineProperty(globalThis, 'Crypto', {
value: function Crypto() {
throw new TypeError('Illegal constructor');
},
writable: true,
enumerable: false,
configurable: true,
});
});