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?

4
  • Just curious to know. Why do you need to calculate SHA256 of files above 400MB in size? Commented Sep 21, 2016 at 6:11
  • 1
    This is because a video file created by iPhone can easy be more than 400MB. Commented Sep 22, 2016 at 3:11
  • 1
    You can always create a native module, i.e. one written in Java on Android, and on iOS accordingly. There is code for Android right here., see the answer on the bottom for a complete function. Commented Oct 7, 2016 at 18:28
  • 1
    Since May 2017 there is this package: github.com/itinance/react-native-sha256 Commented Jul 22, 2017 at 14:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If anyone still looking same issue on 2024 and using Expo, you can just try Expo Crypto: https://docs.expo.dev/versions/latest/sdk/crypto/

const sha256Text = await Crypto.digestStringAsync(
Crypto.CryptoDigestAlgorithm.SHA256, 
'Example text');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.