4

I need to calculate SHA256 hash value of many files in Android and iOS using React Native. When files are selected by a user, my app will start calculating SHA256 for each file.

For web pages, I have been using crypto-js. But for Andoid and iOS applications, I am concerned that performance of crypto-js may not be fast enough as a file (eg. video file) may be >400 MB.

Is there any way that I can call Android/iOS native api to calculate SHA256, instead of using crypto-js for performance sake?

user3390906
  • 147
  • 1
  • 3
  • 12
  • Just curious to know. Why do you need to calculate SHA256 of files above 400MB in size? – Jickson Sep 21 '16 at 06:11
  • 1
    This is because a video file created by iPhone can easy be more than 400MB. – user3390906 Sep 22 '16 at 03:11
  • 1
    You can always create a native module, i.e. [one written in Java on Android](https://facebook.github.io/react-native/docs/native-modules-android.html), and on iOS accordingly. [There is code for Android right here.](http://stackoverflow.com/questions/10129311/does-every-android-phone-support-sha-256), see the answer on the bottom for a complete function. – Mörre Oct 07 '16 at 18:28
  • 1
    Since May 2017 there is this package: https://github.com/itinance/react-native-sha256 – Mörre Jul 22 '17 at 14:35

1 Answers1

5

There isn't anything built into react-native itself to natively calculate a sha-256, but there are a few options.

As @Morre pointed out, react-native-sha256 is an open source project that supports calculating the sha-256 of a string using native components.

Another open source react-native project - react-native-fs - has support for calculating the sha-256 of a file using native components, specifically the hash function.

As @Morre has pointed out, you could write your own native code to provide the same functionality if you would prefer that option. Both of the libraries I mentioned here are open source, so the source code there can be a good reference for what the specific Android/iOS code would need to be written. There are also code examples here on StackOverflow for both java and swift. React-Native's native module documentation (Android) and iOS is extensive.

davidpricedev
  • 2,107
  • 2
  • 20
  • 34