32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
"use strict";
|
|
// Copyright (C) 2016 Dmitry Chestnykh
|
|
// MIT License. See LICENSE file for details.
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.BrowserRandomSource = void 0;
|
|
const QUOTA = 65536;
|
|
class BrowserRandomSource {
|
|
constructor() {
|
|
this.isAvailable = false;
|
|
this.isInstantiated = false;
|
|
const browserCrypto = typeof self !== 'undefined'
|
|
? (self.crypto || self.msCrypto) // IE11 has msCrypto
|
|
: null;
|
|
if (browserCrypto && browserCrypto.getRandomValues !== undefined) {
|
|
this._crypto = browserCrypto;
|
|
this.isAvailable = true;
|
|
this.isInstantiated = true;
|
|
}
|
|
}
|
|
randomBytes(length) {
|
|
if (!this.isAvailable || !this._crypto) {
|
|
throw new Error("Browser random byte generator is not available.");
|
|
}
|
|
const out = new Uint8Array(length);
|
|
for (let i = 0; i < out.length; i += QUOTA) {
|
|
this._crypto.getRandomValues(out.subarray(i, i + Math.min(out.length - i, QUOTA)));
|
|
}
|
|
return out;
|
|
}
|
|
}
|
|
exports.BrowserRandomSource = BrowserRandomSource;
|
|
//# sourceMappingURL=browser.js.map
|