Skip to content

Commit 9a82ec3

Browse files
committed
Assignable polyfill for older mobile browsers
1 parent 6a9447d commit 9a82ec3

4 files changed

Lines changed: 75 additions & 42 deletions

File tree

bcrypt.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,10 @@
638638
}
639639

640640
// Async
641-
if (typeof callback != 'undefined') {
641+
if (typeof callback !== 'undefined') {
642642
next();
643643
return null;
644-
// Sync
644+
// Sync
645645
} else {
646646
var res;
647647
while (true) {
@@ -758,7 +758,13 @@
758758
// Browser, see: http://www.w3.org/TR/WebCryptoAPI/
759759
} else {
760760
var array = new Uint32Array(len);
761-
global.crypto.getRandomValues(array); // Maybe use a polyfill
761+
if (global.crypto && typeof global.crypto.getRandomValues === 'function') {
762+
global.crypto.getRandomValues(array);
763+
} else if (typeof _getRandomValues === 'function') {
764+
_getRandomValues(array);
765+
} else {
766+
throw(new Error("Failed to generate random values: Web Crypto API not available / no polyfill set"));
767+
}
762768
return Array.prototype.slice.call(array);
763769
}
764770
}
@@ -787,6 +793,16 @@
787793
throw(err);
788794
}
789795
}
796+
797+
var _getRandomValues = null;
798+
799+
/**
800+
* Sets the polyfill that should be used if window.crypto.getRandomValues is not available.
801+
* @param {function(Uint32Array)} getRandomValues The actual implementation
802+
*/
803+
bcrypt.setRandomPolyfill = function(getRandomValues) {
804+
_getRandomValues = getRandomValues;
805+
};
790806

791807
/**
792808
* Synchronously generates a salt.

0 commit comments

Comments
 (0)