|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const nodeCrypto = require("crypto"); |
| 4 | +const DOMException = require("domexception/webidl2js-wrapper"); |
| 5 | + |
| 6 | +// https://w3c.github.io/webcrypto/#crypto-interface |
| 7 | +class CryptoImpl { |
| 8 | + constructor(globalObject) { |
| 9 | + this._globalObject = globalObject; |
| 10 | + } |
| 11 | + |
| 12 | + // https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues |
| 13 | + getRandomValues(array) { |
| 14 | + // Note: this rejects Float32Array, Float64Array and DataView. |
| 15 | + // |
| 16 | + // We perform "instance tests" by comparing `array.constructor.name` so |
| 17 | + // that the tests will be successful across realms. |
| 18 | + const typeName = array.constructor.name; |
| 19 | + if (!(typeName === "Int8Array" || |
| 20 | + typeName === "Uint8Array" || |
| 21 | + typeName === "Uint8ClampedArray" || |
| 22 | + typeName === "Int16Array" || |
| 23 | + typeName === "Uint16Array" || |
| 24 | + typeName === "Int32Array" || |
| 25 | + typeName === "Uint32Array" || |
| 26 | + typeName === "BigInt64Array" || |
| 27 | + typeName === "BigUint64Array")) { |
| 28 | + throw DOMException.create(this._globalObject, [ |
| 29 | + `getRandomValues() only accepts integer typed arrays`, |
| 30 | + "TypeMismatchError" |
| 31 | + ]); |
| 32 | + } |
| 33 | + |
| 34 | + if (array.byteLength > 65536) { |
| 35 | + throw DOMException.create(this._globalObject, [ |
| 36 | + `getRandomValues() cannot generate more than 65536 bytes of random values; ` + |
| 37 | + `${array.byteLength} bytes were requested`, |
| 38 | + "QuotaExceededError" |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
| 42 | + nodeCrypto.randomFillSync(array); |
| 43 | + return array; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +exports.implementation = CryptoImpl; |
0 commit comments