Installations
npm install react-native-encrypted-storage
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
16.14.2
NPM Version
8.15.0
Statistics
572 Stars
114 Commits
77 Forks
9 Watching
10 Branches
5 Contributors
Updated on 20 Nov 2024
Bundle Size
683.00 B
Minified
352.00 B
Minified + Gzipped
Languages
TypeScript (36.68%)
Java (35.82%)
Objective-C (20.26%)
JavaScript (3.67%)
Ruby (3.13%)
C (0.27%)
Swift (0.18%)
Total Downloads
Cumulative downloads
Total Downloads
5,272,867
Last day
3.6%
14,652
Compared to previous day
Last week
6.8%
68,616
Compared to previous week
Last month
21.2%
272,361
Compared to previous month
Last year
32.8%
2,259,918
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
React Native Encrypted Storage
React Native wrapper around SharedPreferences and Keychain to provide a secure alternative to Async Storage.
Why ?
Async Storage is great but it lacks security. This is less than ideal when storing sensitive data such as access tokens, payment information and so on. This module aims to solve this problem by providing a wrapper around Android's EncryptedSharedPreferences
and iOS' Keychain
, complete with support for TypeScript.
Version Requirements
- Android API 21+ (5.0)
- iOS 2.0
Installation
Via yarn
1$ yarn add react-native-encrypted-storage
Via npm
1$ npm install react-native-encrypted-storage
Linking
- React Native 0.60+
Since version 0.60, React Native supports auto linking. This means no additional step is needed on your end.
- React Native <= 0.59
1$ react-native link react-native-encrypted-storage
Special note for iOS using cocoapods
, run:
1$ npx pod-install
Usage
This module exposes four (4) native functions to store, retrieve, remove and clear values. They can be used like so:
Import
1import EncryptedStorage from 'react-native-encrypted-storage';
Storing a value
1async function storeUserSession() {
2 try {
3 await EncryptedStorage.setItem(
4 "user_session",
5 JSON.stringify({
6 age : 21,
7 token : "ACCESS_TOKEN",
8 username : "emeraldsanto",
9 languages : ["fr", "en", "de"]
10 })
11 );
12
13 // Congrats! You've just stored your first value!
14 } catch (error) {
15 // There was an error on the native side
16 }
17}
Retrieving a value
1async function retrieveUserSession() { 2 try { 3 const session = await EncryptedStorage.getItem("user_session"); 4 5 if (session !== undefined) { 6 // Congrats! You've just retrieved your first value! 7 } 8 } catch (error) { 9 // There was an error on the native side 10 } 11}
Removing a value
1async function removeUserSession() { 2 try { 3 await EncryptedStorage.removeItem("user_session"); 4 // Congrats! You've just removed your first value! 5 } catch (error) { 6 // There was an error on the native side 7 } 8}
Clearing all previously saved values
1async function clearStorage() { 2 try { 3 await EncryptedStorage.clear(); 4 // Congrats! You've just cleared the device storage! 5 } catch (error) { 6 // There was an error on the native side 7 } 8}
Error handling
Take the removeItem
example, an error can occur when trying to remove a value which does not exist, or for any other reason. This module forwards the native iOS Security framework error codes to help with debugging.
1async function removeUserSession() { 2 try { 3 await EncryptedStorage.removeItem("user_session"); 4 } catch (error) { 5 // There was an error on the native side 6 // You can find out more about this error by using the `error.code` property 7 console.log(error.code); // ex: -25300 (errSecItemNotFound) 8 } 9}
Note regarding Keychain
persistence
You'll notice that the iOS Keychain
is not cleared when your app is uninstalled, this is the expected behaviour. However, if you do want to achieve a different behaviour, you can use the below snippet to clear the Keychain
on the first launch of your app.
1// AppDelegate.m 2 3/** 4 Deletes all Keychain items accessible by this app if this is the first time the user launches the app 5 */ 6static void ClearKeychainIfNecessary() { 7 // Checks wether or not this is the first time the app is run 8 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HAS_RUN_BEFORE"] == NO) { 9 // Set the appropriate value so we don't clear next time the app is launched 10 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HAS_RUN_BEFORE"]; 11 12 NSArray *secItemClasses = @[ 13 (__bridge id)kSecClassGenericPassword, 14 (__bridge id)kSecClassInternetPassword, 15 (__bridge id)kSecClassCertificate, 16 (__bridge id)kSecClassKey, 17 (__bridge id)kSecClassIdentity 18 ]; 19 20 // Maps through all Keychain classes and deletes all items that match 21 for (id secItemClass in secItemClasses) { 22 NSDictionary *spec = @{(__bridge id)kSecClass: secItemClass}; 23 SecItemDelete((__bridge CFDictionaryRef)spec); 24 } 25 } 26} 27 28@implementation AppDelegate 29 30- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 31{ 32 // Add this line to call the above function 33 ClearKeychainIfNecessary(); 34 35 RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; 36 RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"APP_NAME" initialProperties:nil]; 37 38 rootView.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1]; 39 40 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 41 UIViewController *rootViewController = [UIViewController new]; 42 rootViewController.view = rootView; 43 44 self.window.rootViewController = rootViewController; 45 [self.window makeKeyAndVisible]; 46 47 return YES; 48} 49 50// ... 51 52@end
Limitations
There seems to be some confusion around the maximum size of items that can be stored, especially on iOS. According to this StackOverflow question, the actual Keychain limit is much lower than what it should theoretically be. This does not affect Android as the EncryptedSharedPreferences
API relies on the phone's storage, via XML files.
License
MIT
No vulnerabilities found.
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
binaries present in source code
Details
- Warn: binary detected: android/gradle/wrapper/gradle-wrapper.jar:1
- Warn: binary detected: example/android/gradle/wrapper/gradle-wrapper.jar:1
Reason
Found 3/9 approved changesets -- score normalized to 3
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 29 are checked with a SAST tool
Reason
44 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-f5x2-xv93-4p23
- Warn: Project is vulnerable to: GHSA-gmpm-xp43-f7g6
- Warn: Project is vulnerable to: GHSA-pf27-929j-9pmm
- Warn: Project is vulnerable to: GHSA-327c-qx3v-h673
- Warn: Project is vulnerable to: GHSA-x4cf-6jr3-3qvp
- Warn: Project is vulnerable to: GHSA-mph8-6787-r8hw
- Warn: Project is vulnerable to: GHSA-7mhc-prgv-r3q4
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-4cpg-3vgw-4877
- Warn: Project is vulnerable to: GHSA-rxrc-rgv4-jpvx
- Warn: Project is vulnerable to: GHSA-7f53-fmmv-mfjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-gff7-g5r8-mg8m
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-78cj-fxph-m83p
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-h6q6-9hqw-rwfv
- Warn: Project is vulnerable to: GHSA-5fg8-2547-mr8q
- Warn: Project is vulnerable to: GHSA-crh6-fp67-6883
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
2.1
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-native-encrypted-storage
redux-persist-encrypted-storage
Redux Persist storage for React Native Encrypted Storage
pusher-js
Pusher Channels JavaScript library for browsers, React Native, NodeJS and web workers
@react-native-async-storage/async-storage
Asynchronous, persistent, key-value storage system for React Native.
shaka-player
DASH/EME video player library