Skip to content

Instantly share code, notes, and snippets.

@webmaster128
Created March 7, 2018 08:51
Show Gist options
  • Save webmaster128/694f75a9c51750250ba388811c3a6d88 to your computer and use it in GitHub Desktop.
Save webmaster128/694f75a9c51750250ba388811c3a6d88 to your computer and use it in GitHub Desktop.
function installDummyCryptoPolyfill() {
var global = (function(global) {
return global;
})(new Function('return this;')());
try {
global.crypto.getRandomValues(new Uint8Array(1));
return;
}
catch (e) {
console.warn("Installing dummy crypto RNG. This is not secure!");
global.crypto = {
_bytesGenerated: 0,
_bytesMax: 4 + 64,
_getRandomByte: function() {
if (this._bytesGenerated >= this._bytesMax) {
throw new Error("This RNG can only generate " + this._bytesMax + " bytes");
}
this._bytesGenerated += 1;
return 0x00;
},
getRandomValues: function(arrayBufferView) {
var newBytes = [];
for (var i = 0; i < arrayBufferView.byteLength; ++i) {
newBytes.push(this._getRandomByte());
}
new Uint8Array(arrayBufferView.buffer, arrayBufferView.byteOffset, arrayBufferView.byteLength).set(newBytes);
},
};
}
}
installDummyCryptoPolyfill();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment