Created
March 7, 2018 08:51
-
-
Save webmaster128/694f75a9c51750250ba388811c3a6d88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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