react-nativeshacryptojs

SHA256 in React Native


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?


Solution

  • 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.