Rename to react-native-securerandom

This commit is contained in:
rh389
2017-10-11 19:33:26 +01:00
parent d39e6609d0
commit 836381f1fe
17 changed files with 76 additions and 75 deletions
+14 -13
View File
@@ -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\<Uint8Array\>
### generateSecureRandom(length: number) => Promise\<Uint8Array\>
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')
```
+1 -1
View File
@@ -1,4 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.rhogan.rnrandombytes">
package="net.rhogan.rnsecurerandom">
</manifest>
@@ -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 com.facebook.react.bridge.Promise;
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 class RNRandomBytesModule extends ReactContextBaseJavaModule {
@Override
public String getName() {
return "RNRandomBytes";
return "RNSecureRandom";
}
}
@@ -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.NativeModule;
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<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNRandomBytesModule(reactContext));
return Arrays.<NativeModule>asList(new RNSecureRandomModule(reactContext));
}
// Deprecated from RN 0.47
+6 -6
View File
@@ -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<Uint8Array> {
export function generateSecureRandom(length: number): Promise<Uint8Array> {
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));
}
+1 -1
View File
@@ -4,6 +4,6 @@
#import <React/RCTBridgeModule.h>
#endif
@interface RNRandomBytes : NSObject <RCTBridgeModule>
@interface RNSecureRandom : NSObject <RCTBridgeModule>
@end
+4 -4
View File
@@ -1,10 +1,10 @@
#import <Foundation/Foundation.h>
#import "RNRandomBytes.h"
#import "RNSecureRandom.h"
#import <React/RCTUtils.h>
#import <React/RCTLog.h>
@implementation RNRandomBytes
@implementation RNSecureRandom
RCT_EXPORT_MODULE();
@@ -13,7 +13,7 @@ RCT_EXPORT_MODULE();
return NO;
}
RCT_REMAP_METHOD(generateRandomBytesAsBase64,
RCT_REMAP_METHOD(generateSecureRandomAsBase64,
withLength:(int)length
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
@@ -23,7 +23,7 @@ RCT_REMAP_METHOD(generateRandomBytesAsBase64,
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);
}
}
@@ -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
@@ -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 = "<group>"; };
B3E7B5891CC2AC0600A0062D /* RNRandomBytes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNRandomBytes.m; sourceTree = "<group>"; };
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 = "<group>"; };
B3E7B5891CC2AC0600A0062D /* RNSecureRandom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNSecureRandom.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -42,7 +42,7 @@
134814211AA4EA7D00B7C361 /* Products */ = {
isa = PBXGroup;
children = (
134814201AA4EA6300B7C361 /* libRNRandomBytes.a */,
134814201AA4EA6300B7C361 /* libRNSecureRandom.a */,
);
name = Products;
sourceTree = "<group>";
@@ -50,8 +50,8 @@
58B511D21A9E6C8500147676 = {
isa = PBXGroup;
children = (
B3E7B5881CC2AC0600A0062D /* RNRandomBytes.h */,
B3E7B5891CC2AC0600A0062D /* RNRandomBytes.m */,
B3E7B5881CC2AC0600A0062D /* RNSecureRandom.h */,
B3E7B5891CC2AC0600A0062D /* RNSecureRandom.m */,
134814211AA4EA7D00B7C361 /* Products */,
);
sourceTree = "<group>";
@@ -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 */,
@@ -2,6 +2,6 @@
<Workspace
version = "1.0">
<FileRef
location = "group:RNRandomBytes.xcodeproj">
location = "group:RNSecureRandom.xcodeproj">
</FileRef>
</Workspace>
+4 -4
View File
@@ -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": "*"
}
+1 -1
View File
@@ -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
@@ -5,11 +5,11 @@ using System.Runtime.InteropServices;
// 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("")]
@@ -20,7 +20,7 @@
http://go.microsoft.com/fwlink/?LinkID=391919
-->
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="RNRandomBytes">
<Library Name="RNSecureRandom">
<!-- add directives for your library here -->
+3 -3
View File
@@ -129,9 +129,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RNRandomBytesModule.cs" />
<Compile Include="RNRandomBytesPackage.cs" />
<EmbeddedResource Include="Properties\RNRandomBytes.rd.xml" />
<Compile Include="RNSecureRandomModule.cs" />
<Compile Include="RNSecureRandomPackage.cs" />
<EmbeddedResource Include="Properties\RNSecureRandom.rd.xml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(ReactWindowsRoot)\react-native-windows\ReactWindows\ReactNative\ReactNative.csproj">
+5 -5
View File
@@ -4,17 +4,17 @@ using System.Collections.Generic;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
namespace Net.Rhogan.RNRandomBytes
namespace Net.Rhogan.RNSecureRandom
{
/// <summary>
/// A module that allows JS to share data.
/// </summary>
class RNRandomBytesModule : NativeModuleBase
class RNSecureRandomModule : NativeModuleBase
{
/// <summary>
/// Instantiates the <see cref="RNRandomBytesModule"/>.
/// Instantiates the <see cref="RNSecureRandomModule"/>.
/// </summary>
internal RNRandomBytesModule()
internal RNSecureRandomModule()
{
}
@@ -26,7 +26,7 @@ namespace Net.Rhogan.RNRandomBytes
{
get
{
return "RNRandomBytes";
return "RNSecureRandom";
}
}
}
@@ -4,7 +4,7 @@ using ReactNative.UIManager;
using System;
using System.Collections.Generic;
namespace Net.Rhogan.RNRandomBytes
namespace Net.Rhogan.RNSecureRandom
{
/// <summary>
/// Package defining core framework modules (e.g., <see cref="UIManagerModule"/>).
@@ -12,7 +12,7 @@ namespace Net.Rhogan.RNRandomBytes
/// other framework parts (e.g., with the list of packages to load view
/// managers from).
/// </summary>
public class RNRandomBytesPackage : IReactPackage
public class RNSecureRandomPackage : IReactPackage
{
/// <summary>
/// Creates the list of native modules to register with the react
@@ -24,7 +24,7 @@ namespace Net.Rhogan.RNRandomBytes
{
return new List<INativeModule>
{
new RNRandomBytesModule(),
new RNSecureRandomModule(),
};
}