UNPKG

cloudworker-proxy

Version:
33 lines (32 loc) 1.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const crypto = require("crypto"); const webcrypto = require("webcrypto-core"); const key_storage_1 = require("./key_storage"); const subtle = require("./subtle"); const ERR_RANDOM_VALUE_LENGTH = "Failed to execute 'getRandomValues' on 'Crypto': The ArrayBufferView's byte length (%1) exceeds the number of bytes of entropy available via this API (65536)."; const ERR_RANDOM_NO_VALUE = "Failed to execute 'getRandomValues' on 'Crypto': Expected ArrayBufferView but got %s."; class WebCrypto { constructor(options) { this.subtle = new subtle.SubtleCrypto(); if (options && options.directory) { this.keyStorage = new key_storage_1.KeyStorage(options.directory); } } getRandomValues(array) { if (array) { if (array.byteLength > 65536) { const error = new webcrypto.WebCryptoError(ERR_RANDOM_VALUE_LENGTH, array.byteLength); error.code = 22; throw error; } const bytes = crypto.randomBytes(array.byteLength); array.set(new array.constructor(bytes.buffer)); return array; } else { throw new webcrypto.WebCryptoError(ERR_RANDOM_NO_VALUE, array); } } } module.exports = WebCrypto;