Native SHA256/SHA1 Hashing for React Native
react-native-sha256 provides performant, native SHA256 and SHA1 hashing capabilities for strings and byte arrays within React Native applications. Leveraging native modules on both iOS and Android, it significantly outperforms JavaScript-only implementations in terms of speed, which is crucial for mobile performance. The current stable version is 1.4.10, with releases occurring sporadically based on maintenance and feature additions. Key differentiators include its native execution, support for both SHA256 and SHA1 (since version 1.3.6), and built-in TypeScript definitions, making it suitable for modern React Native development. While it supports traditional `react-native link` for older projects, it also outlines CocoaPods integration for iOS, aligning with contemporary React Native setups. For file hashing, it suggests integration with `react-native-fs` rather than providing its own implementation.
Common errors
-
Invariant Violation: Native module RNSha256 not found. Did you link all the native dependencies and rebuild your app?
cause The native module for react-native-sha256 was not correctly linked or built into your application.fixFor React Native <0.60: Run `react-native link react-native-sha256`. For React Native >=0.60: Ensure auto-linking is working; navigate to `ios/` and run `pod install`. For Android, ensure Gradle builds correctly. Clean your build caches and rebuild the native project. -
ld: framework not found RNSha256 for architecture x86_64
cause iOS linker error indicating the RNSha256 framework (or library) is missing from the Xcode build.fixRun `cd ios && pod install` to ensure CocoaPods integrates the library. If using manual linking, verify the `.xcodeproj` file is added to your Libraries in Xcode and the target's 'Build Phases' link binary with libraries includes `libRNSha256.a`. -
TypeError: null is not an object (evaluating 'RNSha256.sha256')
cause The JavaScript bridge to the native module is `null`, meaning the native module could not be loaded or initialized at runtime.fixThis typically points to the same underlying linking issues as 'Native module RNSha256 not found'. Verify installation steps, clear Metro cache (`npm start --reset-cache`), and perform a clean native build (`cd ios && rm -rf build && pod install && cd .. && npx react-native run-ios` for iOS).
Warnings
- breaking For React Native versions 0.60 and above, `react-native link` is deprecated in favor of auto-linking. Manual linking or specific CocoaPods configurations might be necessary.
- gotcha This library provides native modules and requires proper native project setup (CocoaPods for iOS, Gradle for Android). Forgetting `pod install` or `react-native link` (for older RN versions) will result in runtime errors.
- gotcha The `sha256Bytes` method expects an `Array<number>`, not a `Uint8Array` directly. You must convert `Uint8Array` instances using `Array.from()`.
- deprecated While SHA-1 is supported, it is cryptographically broken and should not be used for security-critical applications like password hashing or digital signatures. Use SHA-256 instead.
Install
-
npm install react-native-sha256 -
yarn add react-native-sha256 -
pnpm add react-native-sha256
Imports
- sha256
const { sha256 } = require('react-native-sha256');import { sha256 } from 'react-native-sha256'; - sha256Bytes
import { sha256Bytes } from 'react-native-sha256'; - sha1
import sha1 from 'react-native-sha256';
import { sha1 } from 'react-native-sha256';
Quickstart
import { sha256 } from 'react-native-sha256';
// Hash a simple string
sha256("Hello, checklist.day!").then(hash => {
console.log('SHA256 Hash:', hash);
}).catch(error => {
console.error('Hashing error:', error);
});
// For hashing byte arrays, prepare the array
const messageBytes = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]); // 'Hello World'
import { sha256Bytes } from 'react-native-sha256';
sha256Bytes(Array.from(messageBytes)).then(hash => {
console.log('SHA256 Bytes Hash:', hash);
}).catch(error => {
console.error('Hashing bytes error:', error);
});