Skip to content

Commit

Permalink
Rename to react-native-securerandom
Browse files Browse the repository at this point in the history
  • Loading branch information
robhogan committed Oct 11, 2017
1 parent d39e660 commit 836381f
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 75 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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')
```
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.rhogan.rnrandombytes">
package="net.rhogan.rnsecurerandom">

</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.rhogan.rnrandombytes;
package net.rhogan.rnsecurerandom;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -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);
Expand All @@ -27,6 +27,6 @@ public void generateRandomBytesAsBase64(int length, Promise promise) {

@Override
public String getName() {
return "RNRandomBytes";
return "RNSecureRandom";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.rhogan.rnrandombytes;
package net.rhogan.rnsecurerandom;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -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<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNRandomBytesModule(reactContext));
return Arrays.<NativeModule>asList(new RNSecureRandomModule(reactContext));
}

// Deprecated from RN 0.47
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
2 changes: 1 addition & 1 deletion ios/RNRandomBytes.h → ios/RNSecureRandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#import <React/RCTBridgeModule.h>
#endif

@interface RNRandomBytes : NSObject <RCTBridgeModule>
@interface RNSecureRandom : NSObject <RCTBridgeModule>

@end
8 changes: 4 additions & 4 deletions ios/RNRandomBytes.m → ios/RNSecureRandom.m
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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)
Expand All @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions ios/RNRandomBytes.podspec → ios/RNSecureRandom.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -42,26 +42,26 @@
134814211AA4EA7D00B7C361 /* Products */ = {
isa = PBXGroup;
children = (
134814201AA4EA6300B7C361 /* libRNRandomBytes.a */,
134814201AA4EA6300B7C361 /* libRNSecureRandom.a */,
);
name = Products;
sourceTree = "<group>";
};
58B511D21A9E6C8500147676 = {
isa = PBXGroup;
children = (
B3E7B5881CC2AC0600A0062D /* RNRandomBytes.h */,
B3E7B5891CC2AC0600A0062D /* RNRandomBytes.m */,
B3E7B5881CC2AC0600A0062D /* RNSecureRandom.h */,
B3E7B5891CC2AC0600A0062D /* RNSecureRandom.m */,
134814211AA4EA7D00B7C361 /* Products */,
);
sourceTree = "<group>";
};
/* 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 */,
Expand All @@ -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 */
Expand All @@ -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;
Expand All @@ -102,7 +102,7 @@
projectDirPath = "";
projectRoot = "";
targets = (
58B511DA1A9E6C8500147676 /* RNRandomBytes */,
58B511DA1A9E6C8500147676 /* RNSecureRandom */,
);
};
/* End PBXProject section */
Expand All @@ -112,7 +112,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B3E7B58A1CC2AC0600A0062D /* RNRandomBytes.m in Sources */,
B3E7B58A1CC2AC0600A0062D /* RNSecureRandom.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -211,7 +211,7 @@
);
LIBRARY_SEARCH_PATHS = "$(inherited)";
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = RNRandomBytes;
PRODUCT_NAME = RNSecureRandom;
SKIP_INSTALL = YES;
};
name = Debug;
Expand All @@ -227,15 +227,15 @@
);
LIBRARY_SEARCH_PATHS = "$(inherited)";
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = RNRandomBytes;
PRODUCT_NAME = RNSecureRandom;
SKIP_INSTALL = YES;
};
name = Release;
};
/* 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 */,
Expand All @@ -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 */,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand All @@ -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": "*"
}
Expand Down
2 changes: 1 addition & 1 deletion windows/RNRandomBytes.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions windows/RNRandomBytes/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("")]
Expand Down
2 changes: 1 addition & 1 deletion windows/RNRandomBytes/Properties/RNRandomBytes.rd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand Down
Loading

0 comments on commit 836381f

Please sign in to comment.