dcodeIO / bcrypt.js

Optimized bcrypt in plain JavaScript with zero dependencies.
Other
3.5k stars 266 forks source link

setRandomFallback doesn't work in environments that support commonJS but are not Node.js #33

Open emiraydin opened 8 years ago

emiraydin commented 8 years ago

I am working on a React Native project to build a mobile app. React Native essentially runs on JavaScriptCore for iOS and Android devices which is the JS engine that powers Safari (https://facebook.github.io/react-native/docs/javascript-environment.html).

The problem is, this environment does support commonJS style requires & exports, but since it doesn't run on Node.js environment, it doesn't have Node's core modules like crypto. This causes the setRandomFallback to not work even if I set it and throw a module not found error for crypto. This is because the way fallback is defined within random function, the first check looks for this: (typeof module !== 'undefined' && module && module['exports'])

I'm not sure if you'd like me to add React Native specific check because this is a generic library, but I think we should do this check in a way that supports such environments.

jsdario commented 7 years ago

I am also unable to import this package. So I am requiring the copy named react-native-bcrypt. Any help on how to import it, by the way?

emiraydin commented 7 years ago

@jsdario I actually forked this repo and created aforementioned react-native-bcrypt package myself a while ago. The only way for now seems to use that package or change the randomFallback check to fit your usage. My fork is here: https://github.com/emiraydin/react-native-bcrypt

anshul-kai commented 7 years ago

Hi @emiraydin! Thank you for your fork, it helped a lot. Although, I had to spend hours figuring out why this won't work for release builds on both Android and iOS.

Uint8Array.prototype.map is not defined on Android. We can either not use map or add something like below in your libaray.

if (!Uint8Array.prototype.map) {
  Uint8Array.prototype.map = Array.prototype.map;
}

This works fine on iOS unless you're using btoa in which case something like below would work.

if (!global.btoa) {
  global.btoa = require('base-64').encode;
}