diff --git a/README.md b/README.md index 896c62f..0bf5df9 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,47 @@ -# react-native-random-bytes +# react-native-securerandom +[![npm version](https://badge.fury.io/js/react-native-securerandom.svg)](https://badge.fury.io/js/react-native-securerandom) A library to generate cryptographically-secure random bytes. Uses `SecRandomCopyBytes` on iOS and `SecureRandom` on Android. ## Usage The library exports a single function: -### generateRandomBytes(length: number) => Promise\ +### generateSecureRandom(length: number) => Promise\ Takes a length, the number of bytes to generate, and returns a `Promise` that resolves with a `Uint8Array`. ```javascript -import { generateRandomBytes } from 'react-native-random-bytes'; +import { generateSecureRandom } from 'react-native-securerandom'; -generateRandomBytes(12).then(randomBytes => console.log(randomBytes)); +generateSecureRandom(12).then(randomBytes => console.log(randomBytes)); ``` ## Installation -`$ yarn add react-native-random-bytes` +`$ yarn add react-native-securerandom` ### Automatic linking with react-native link -`$ react-native link react-native-random-bytes` +`$ react-native link react-native-securerandom` ### Manual linking #### iOS 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` -2. Go to `node_modules` ➜ `react-native-random-bytes` and add `RNRandomBytes.xcodeproj` -3. In XCode, in the project navigator, select your project. Add `libRNRandomBytes.a` to your project's `Build Phases` ➜ `Link Binary With Libraries` +2. Go to `node_modules` ➜ `react-native-securerandom` and add `RNSecureRandom.xcodeproj` +3. In XCode, in the project navigator, select your project. Add `libRNSecureRandom.a` to your project's `Build Phases` ➜ `Link Binary With Libraries` 4. Run your project (`Cmd+R`)< #### Android 1. Open up `android/app/src/main/java/[...]/MainActivity.java` - - Add `import net.rhogan.rnrandombytes.RNRandomBytesPackage;` to the imports at the top of the file - - Add `new RNRandomBytesPackage()` to the list returned by the `getPackages()` method + - Add `import net.rhogan.rnsecurerandom.RNSecureRandomPackage;` to the imports at the top of the file + - Add `new RNSecureRandomPackage()` to the list returned by the `getPackages()` method 2. Append the following lines to `android/settings.gradle`: ``` - include ':react-native-random-bytes' - project(':react-native-random-bytes').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-random-bytes/android') + include ':react-native-securerandom' + project(':react-native-securerandom').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-securerandom/android') ``` 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: ``` - compile project(':react-native-random-bytes') + compile project(':react-native-securerandom') ``` diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index a482623..40622b2 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,4 +1,4 @@ + package="net.rhogan.rnsecurerandom"> diff --git a/android/src/main/java/net/rhogan/rnrandombytes/RNRandomBytesModule.java b/android/src/main/java/net/rhogan/rnrandombytes/RNSecureRandomModule.java similarity index 70% rename from android/src/main/java/net/rhogan/rnrandombytes/RNRandomBytesModule.java rename to android/src/main/java/net/rhogan/rnrandombytes/RNSecureRandomModule.java index 3de1974..da8bc2a 100644 --- a/android/src/main/java/net/rhogan/rnrandombytes/RNRandomBytesModule.java +++ b/android/src/main/java/net/rhogan/rnrandombytes/RNSecureRandomModule.java @@ -1,4 +1,4 @@ -package net.rhogan.rnrandombytes; +package net.rhogan.rnsecurerandom; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -8,17 +8,17 @@ import java.security.SecureRandom; import android.util.Base64; -public class RNRandomBytesModule extends ReactContextBaseJavaModule { +public class RNSecureRandomModule extends ReactContextBaseJavaModule { private final ReactApplicationContext reactContext; - public RNRandomBytesModule(ReactApplicationContext reactContext) { + public RNSecureRandomModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; } @ReactMethod - public void generateRandomBytesAsBase64(int length, Promise promise) { + public void generateSecureRandomAsBase64(int length, Promise promise) { SecureRandom secureRandom = new SecureRandom(); byte[] buffer = new byte[length]; secureRandom.nextBytes(buffer); @@ -27,6 +27,6 @@ public void generateRandomBytesAsBase64(int length, Promise promise) { @Override public String getName() { - return "RNRandomBytes"; + return "RNSecureRandom"; } } diff --git a/android/src/main/java/net/rhogan/rnrandombytes/RNRandomBytesPackage.java b/android/src/main/java/net/rhogan/rnrandombytes/RNSecureRandomPackage.java similarity index 80% rename from android/src/main/java/net/rhogan/rnrandombytes/RNRandomBytesPackage.java rename to android/src/main/java/net/rhogan/rnrandombytes/RNSecureRandomPackage.java index 6c522e2..1fe5281 100644 --- a/android/src/main/java/net/rhogan/rnrandombytes/RNRandomBytesPackage.java +++ b/android/src/main/java/net/rhogan/rnrandombytes/RNSecureRandomPackage.java @@ -1,4 +1,4 @@ -package net.rhogan.rnrandombytes; +package net.rhogan.rnsecurerandom; import java.util.Arrays; import java.util.Collections; @@ -9,10 +9,10 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.uimanager.ViewManager; import com.facebook.react.bridge.JavaScriptModule; -public class RNRandomBytesPackage implements ReactPackage { +public class RNSecureRandomPackage implements ReactPackage { @Override public List createNativeModules(ReactApplicationContext reactContext) { - return Arrays.asList(new RNRandomBytesModule(reactContext)); + return Arrays.asList(new RNSecureRandomModule(reactContext)); } // Deprecated from RN 0.47 diff --git a/index.js b/index.js index dc60c22..ccd6d4e 100644 --- a/index.js +++ b/index.js @@ -3,16 +3,16 @@ import { NativeModules, Platform } from 'react-native'; import { toByteArray } from 'base64-js'; -const { RNRandomBytes } = NativeModules; +const { RNSecureRandom } = NativeModules; -export function generateRandomBytes(length: number): Promise { +export function generateSecureRandom(length: number): Promise { if (Platform.OS !== 'ios' && Platform.OS !== 'android') { - throw Error('react-native-random-bytes is currently only available for iOS and Android'); + throw Error('react-native-securerandom is currently only available for iOS and Android'); } - if (!RNRandomBytes || !RNRandomBytes.generateRandomBytesAsBase64) { - throw Error('react-native-random-bytes is not properly linked'); + if (!RNSecureRandom || !RNSecureRandom.generateSecureRandomAsBase64) { + throw Error('react-native-securerandom is not properly linked'); } - return RNRandomBytes.generateRandomBytesAsBase64(length).then(base64 => toByteArray(base64)); + return RNSecureRandom.generateSecureRandomAsBase64(length).then(base64 => toByteArray(base64)); } diff --git a/ios/RNRandomBytes.h b/ios/RNSecureRandom.h similarity index 68% rename from ios/RNRandomBytes.h rename to ios/RNSecureRandom.h index 3d7f30e..a4377ea 100644 --- a/ios/RNRandomBytes.h +++ b/ios/RNSecureRandom.h @@ -4,6 +4,6 @@ #import #endif -@interface RNRandomBytes : NSObject +@interface RNSecureRandom : NSObject @end diff --git a/ios/RNRandomBytes.m b/ios/RNSecureRandom.m similarity index 76% rename from ios/RNRandomBytes.m rename to ios/RNSecureRandom.m index 13c7115..51a67b3 100644 --- a/ios/RNRandomBytes.m +++ b/ios/RNSecureRandom.m @@ -1,10 +1,10 @@ #import -#import "RNRandomBytes.h" +#import "RNSecureRandom.h" #import #import -@implementation RNRandomBytes +@implementation RNSecureRandom RCT_EXPORT_MODULE(); @@ -13,7 +13,7 @@ + (BOOL)requiresMainQueueSetup return NO; } -RCT_REMAP_METHOD(generateRandomBytesAsBase64, +RCT_REMAP_METHOD(generateSecureRandomAsBase64, withLength:(int)length resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) @@ -23,7 +23,7 @@ + (BOOL)requiresMainQueueSetup if (result == errSecSuccess) { resolve([bytes base64EncodedStringWithOptions:0]); } else { - NSError *error = [NSError errorWithDomain:@"RNRandomBytes" code:result userInfo: nil]; + NSError *error = [NSError errorWithDomain:@"RNSecureRandom" code:result userInfo: nil]; reject(@"randombytes_error", @"Error generating random bytes", error); } } diff --git a/ios/RNRandomBytes.podspec b/ios/RNSecureRandom.podspec similarity index 65% rename from ios/RNRandomBytes.podspec rename to ios/RNSecureRandom.podspec index 5e32f2d..ffdfcab 100644 --- a/ios/RNRandomBytes.podspec +++ b/ios/RNSecureRandom.podspec @@ -3,16 +3,16 @@ require "json" package = JSON.parse(File.read(File.join(__dir__, "package.json"))) Pod::Spec.new do |s| - s.name = "RNRandomBytes" + s.name = "RNSecureRandom" s.version = package["version"] - s.summary = "RNRandomBytes" + s.summary = "RNSecureRandom" s.description = package["description"] s.homepage = package["homepage"] s.license = "MIT" s.author = { "Rob Hogan" => "roberthogan@blueyonder.co.uk" } s.platform = :ios, "7.0" - s.source = { :git => "https://github.com/author/RNRandomBytes.git", :tag => "master" } - s.source_files = "RNRandomBytes/**/*.{h,m}" + s.source = { :git => "https://github.com/author/RNSecureRandom.git", :tag => "master" } + s.source_files = "RNSecureRandom/**/*.{h,m}" s.requires_arc = true s.dependency "React" end diff --git a/ios/RNRandomBytes.xcodeproj/project.pbxproj b/ios/RNSecureRandom.xcodeproj/project.pbxproj similarity index 83% rename from ios/RNRandomBytes.xcodeproj/project.pbxproj rename to ios/RNSecureRandom.xcodeproj/project.pbxproj index 7ae90df..5c998c4 100644 --- a/ios/RNRandomBytes.xcodeproj/project.pbxproj +++ b/ios/RNSecureRandom.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - B3E7B58A1CC2AC0600A0062D /* RNRandomBytes.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RNRandomBytes.m */; }; + B3E7B58A1CC2AC0600A0062D /* RNSecureRandom.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RNSecureRandom.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -23,9 +23,9 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 134814201AA4EA6300B7C361 /* libRNRandomBytes.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNRandomBytes.a; sourceTree = BUILT_PRODUCTS_DIR; }; - B3E7B5881CC2AC0600A0062D /* RNRandomBytes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNRandomBytes.h; sourceTree = ""; }; - B3E7B5891CC2AC0600A0062D /* RNRandomBytes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNRandomBytes.m; sourceTree = ""; }; + 134814201AA4EA6300B7C361 /* libRNSecureRandom.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNSecureRandom.a; sourceTree = BUILT_PRODUCTS_DIR; }; + B3E7B5881CC2AC0600A0062D /* RNSecureRandom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNSecureRandom.h; sourceTree = ""; }; + B3E7B5891CC2AC0600A0062D /* RNSecureRandom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNSecureRandom.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -42,7 +42,7 @@ 134814211AA4EA7D00B7C361 /* Products */ = { isa = PBXGroup; children = ( - 134814201AA4EA6300B7C361 /* libRNRandomBytes.a */, + 134814201AA4EA6300B7C361 /* libRNSecureRandom.a */, ); name = Products; sourceTree = ""; @@ -50,8 +50,8 @@ 58B511D21A9E6C8500147676 = { isa = PBXGroup; children = ( - B3E7B5881CC2AC0600A0062D /* RNRandomBytes.h */, - B3E7B5891CC2AC0600A0062D /* RNRandomBytes.m */, + B3E7B5881CC2AC0600A0062D /* RNSecureRandom.h */, + B3E7B5891CC2AC0600A0062D /* RNSecureRandom.m */, 134814211AA4EA7D00B7C361 /* Products */, ); sourceTree = ""; @@ -59,9 +59,9 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ - 58B511DA1A9E6C8500147676 /* RNRandomBytes */ = { + 58B511DA1A9E6C8500147676 /* RNSecureRandom */ = { isa = PBXNativeTarget; - buildConfigurationList = 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNRandomBytes" */; + buildConfigurationList = 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNSecureRandom" */; buildPhases = ( 58B511D71A9E6C8500147676 /* Sources */, 58B511D81A9E6C8500147676 /* Frameworks */, @@ -71,9 +71,9 @@ ); dependencies = ( ); - name = RNRandomBytes; + name = RNSecureRandom; productName = RCTDataManager; - productReference = 134814201AA4EA6300B7C361 /* libRNRandomBytes.a */; + productReference = 134814201AA4EA6300B7C361 /* libRNSecureRandom.a */; productType = "com.apple.product-type.library.static"; }; /* End PBXNativeTarget section */ @@ -90,7 +90,7 @@ }; }; }; - buildConfigurationList = 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNRandomBytes" */; + buildConfigurationList = 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNSecureRandom" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; @@ -102,7 +102,7 @@ projectDirPath = ""; projectRoot = ""; targets = ( - 58B511DA1A9E6C8500147676 /* RNRandomBytes */, + 58B511DA1A9E6C8500147676 /* RNSecureRandom */, ); }; /* End PBXProject section */ @@ -112,7 +112,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - B3E7B58A1CC2AC0600A0062D /* RNRandomBytes.m in Sources */, + B3E7B58A1CC2AC0600A0062D /* RNSecureRandom.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -211,7 +211,7 @@ ); LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = "-ObjC"; - PRODUCT_NAME = RNRandomBytes; + PRODUCT_NAME = RNSecureRandom; SKIP_INSTALL = YES; }; name = Debug; @@ -227,7 +227,7 @@ ); LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = "-ObjC"; - PRODUCT_NAME = RNRandomBytes; + PRODUCT_NAME = RNSecureRandom; SKIP_INSTALL = YES; }; name = Release; @@ -235,7 +235,7 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ - 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNRandomBytes" */ = { + 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNSecureRandom" */ = { isa = XCConfigurationList; buildConfigurations = ( 58B511ED1A9E6C8500147676 /* Debug */, @@ -244,7 +244,7 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; - 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNRandomBytes" */ = { + 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNSecureRandom" */ = { isa = XCConfigurationList; buildConfigurations = ( 58B511F01A9E6C8500147676 /* Debug */, diff --git a/ios/RNRandomBytes.xcworkspace/contents.xcworkspacedata b/ios/RNSecureRandom.xcworkspace/contents.xcworkspacedata similarity index 68% rename from ios/RNRandomBytes.xcworkspace/contents.xcworkspacedata rename to ios/RNSecureRandom.xcworkspace/contents.xcworkspacedata index 520d0e1..52247d8 100644 --- a/ios/RNRandomBytes.xcworkspace/contents.xcworkspacedata +++ b/ios/RNSecureRandom.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "group:RNSecureRandom.xcodeproj"> diff --git a/package.json b/package.json index 426e010..eac5c41 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@rh389/react-native-random-bytes", - "version": "0.1.0", + "name": "react-native-securerandom", + "version": "0.1.1", "description":"Generate cryptographically-secure random bytes in react native", "main": "index.js", "scripts": { @@ -13,7 +13,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/rh389/react-native-random-bytes.git" + "url": "git+https://github.com/rh389/react-native-securerandom.git" }, "keywords": [ "react-native", @@ -23,7 +23,7 @@ "SecureRandom", "crypto" ], - "homepage": "https://github.com/rh389/react-native-random-bytes#readme", + "homepage": "https://github.com/rh389/react-native-securerandom#readme", "dependencies": { "base64-js": "*" } diff --git a/windows/RNRandomBytes.sln b/windows/RNRandomBytes.sln index d5381ae..18e9070 100644 --- a/windows/RNRandomBytes.sln +++ b/windows/RNRandomBytes.sln @@ -2,7 +2,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 VisualStudioVersion = 14.0.25123.0 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RNRandomBytes", "RNRandomBytes\RNRandomBytes.csproj", "{78648660-AE9A-11E7-AB46-9DDA6E026AA1}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RNSecureRandom", "RNSecureRandom\RNSecureRandom.csproj", "{78648660-AE9A-11E7-AB46-9DDA6E026AA1}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactNative", "..\node_modules\react-native-windows\ReactWindows\ReactNative\ReactNative.csproj", "{C7673AD5-E3AA-468C-A5FD-FA38154E205C}" EndProject diff --git a/windows/RNRandomBytes/Properties/AssemblyInfo.cs b/windows/RNRandomBytes/Properties/AssemblyInfo.cs index ba559cd..667c7b0 100644 --- a/windows/RNRandomBytes/Properties/AssemblyInfo.cs +++ b/windows/RNRandomBytes/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("RNRandomBytes")] +[assembly: AssemblyTitle("RNSecureRandom")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("RNRandomBytes")] +[assembly: AssemblyProduct("RNSecureRandom")] [assembly: AssemblyCopyright("Copyright © 2016")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/windows/RNRandomBytes/Properties/RNRandomBytes.rd.xml b/windows/RNRandomBytes/Properties/RNRandomBytes.rd.xml index 0f47a9e..0f7a289 100644 --- a/windows/RNRandomBytes/Properties/RNRandomBytes.rd.xml +++ b/windows/RNRandomBytes/Properties/RNRandomBytes.rd.xml @@ -20,7 +20,7 @@ http://go.microsoft.com/fwlink/?LinkID=391919 --> - + diff --git a/windows/RNRandomBytes/RNRandomBytes.csproj b/windows/RNRandomBytes/RNRandomBytes.csproj index a772925..9a4a2b4 100644 --- a/windows/RNRandomBytes/RNRandomBytes.csproj +++ b/windows/RNRandomBytes/RNRandomBytes.csproj @@ -129,9 +129,9 @@ - - - + + + diff --git a/windows/RNRandomBytes/RNRandomBytesModule.cs b/windows/RNRandomBytes/RNRandomBytesModule.cs index 1a43911..16f461f 100644 --- a/windows/RNRandomBytes/RNRandomBytesModule.cs +++ b/windows/RNRandomBytes/RNRandomBytesModule.cs @@ -4,17 +4,17 @@ using Windows.ApplicationModel.Core; using Windows.UI.Core; -namespace Net.Rhogan.RNRandomBytes +namespace Net.Rhogan.RNSecureRandom { /// /// A module that allows JS to share data. /// - class RNRandomBytesModule : NativeModuleBase + class RNSecureRandomModule : NativeModuleBase { /// - /// Instantiates the . + /// Instantiates the . /// - internal RNRandomBytesModule() + internal RNSecureRandomModule() { } @@ -26,7 +26,7 @@ public override string Name { get { - return "RNRandomBytes"; + return "RNSecureRandom"; } } } diff --git a/windows/RNRandomBytes/RNRandomBytesPackage.cs b/windows/RNRandomBytes/RNRandomBytesPackage.cs index b117670..bb0be0b 100644 --- a/windows/RNRandomBytes/RNRandomBytesPackage.cs +++ b/windows/RNRandomBytes/RNRandomBytesPackage.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; -namespace Net.Rhogan.RNRandomBytes +namespace Net.Rhogan.RNSecureRandom { /// /// Package defining core framework modules (e.g., ). @@ -12,7 +12,7 @@ namespace Net.Rhogan.RNRandomBytes /// other framework parts (e.g., with the list of packages to load view /// managers from). /// - public class RNRandomBytesPackage : IReactPackage + public class RNSecureRandomPackage : IReactPackage { /// /// Creates the list of native modules to register with the react @@ -24,7 +24,7 @@ public IReadOnlyList CreateNativeModules(ReactContext reactContex { return new List { - new RNRandomBytesModule(), + new RNSecureRandomModule(), }; }