Skip to content

Commit 21df64e

Browse files
authored
Merge pull request #57 from bdpdx/bdpdx/add-hash-with-bytes-methods
Add methods to get the hash of a byte array
2 parents af438df + 5bd20f5 commit 21df64e

File tree

5 files changed

+118
-1
lines changed

5 files changed

+118
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pod install
2929
Import the lib into your project:
3030

3131
```javascript
32-
import { sha256 } from 'react-native-sha256';
32+
import { sha256, sha256Bytes } from 'react-native-sha256';
3333
```
3434

3535
Build a sha256-hash:
@@ -40,6 +40,15 @@ sha256("Test").then( hash => {
4040
})
4141
```
4242

43+
```javascript
44+
const message = new Uint8Array(8);
45+
const bytes = Array.from(message);
46+
47+
sha256Bytes(message).then( hash => {
48+
console.log(hash);
49+
})
50+
```
51+
4352
# File-Hashes
4453

4554
If you need to calculate SHA-256 hashes from a file, use this method of react-native-fs:

android/src/main/java/com/sha256lib/Sha256Module.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.facebook.react.bridge.ReactApplicationContext;
55
import com.facebook.react.bridge.ReactContextBaseJavaModule;
66
import com.facebook.react.bridge.ReactMethod;
7+
import com.facebook.react.bridge.ReadableArray;
78
import com.facebook.react.bridge.Callback;
89
import com.facebook.react.bridge.Promise;
910

@@ -28,13 +29,29 @@ public String getName() {
2829
return "sha256Lib";
2930
}
3031

32+
private static byte[] readableArrayToByteArray(ReadableArray readableArray) {
33+
byte[] arr = new byte[readableArray.size()];
34+
for (int i = 0; i < readableArray.size(); ++i) {
35+
arr[i] = (byte) readableArray.getInt(i);
36+
}
37+
38+
return arr;
39+
}
40+
3141
String buildHash(final String toHash, final String algo, final Integer length) throws NoSuchAlgorithmException, UnsupportedEncodingException {
3242
MessageDigest md = MessageDigest.getInstance(algo);
3343
md.update(toHash.getBytes("UTF-8"));
3444
byte[] digest = md.digest();
3545
return String.format("%0" + length.toString() + "x", new java.math.BigInteger(1, digest));
3646
}
3747

48+
String buildHashWithBytes(final ReadableArray toHash, final String algo, final Integer length) throws NoSuchAlgorithmException, UnsupportedEncodingException {
49+
MessageDigest md = MessageDigest.getInstance(algo);
50+
byte[] arr = Sha256Module.readableArrayToByteArray(toHash);
51+
md.update(arr);
52+
byte[] digest = md.digest();
53+
return String.format("%0" + length.toString() + "x", new java.math.BigInteger(1, digest));
54+
}
3855

3956
@ReactMethod
4057
public void sha256(final String toHash, Promise promise) {
@@ -50,6 +67,20 @@ public void sha256(final String toHash, Promise promise) {
5067
}
5168
}
5269

70+
@ReactMethod
71+
public void sha256Bytes(final ReadableArray toHash, Promise promise) {
72+
try {
73+
String hash = buildHashWithBytes(toHash, "SHA-256", 64);
74+
promise.resolve(hash);
75+
} catch (NoSuchAlgorithmException e) {
76+
e.printStackTrace();
77+
promise.reject("sha256", e.getMessage());
78+
} catch (UnsupportedEncodingException e) {
79+
e.printStackTrace();
80+
promise.reject("sha256", e.getMessage());
81+
}
82+
}
83+
5384
@ReactMethod
5485
public void sha1(final String toHash, Promise promise) {
5586
try {
@@ -64,4 +95,18 @@ public void sha1(final String toHash, Promise promise) {
6495
}
6596
}
6697

98+
@ReactMethod
99+
public void sha1Bytes(final ReadableArray toHash, Promise promise) {
100+
try {
101+
String hash = buildHashWithBytes(toHash, "SHA-1", 40);
102+
promise.resolve(hash);
103+
} catch (NoSuchAlgorithmException e) {
104+
e.printStackTrace();
105+
promise.reject("sha1", e.getMessage());
106+
} catch (UnsupportedEncodingException e) {
107+
e.printStackTrace();
108+
promise.reject("sha1", e.getMessage());
109+
}
110+
}
111+
67112
}

ios/sha256.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@ - (NSString*) calcHash: (NSString*) subject withDigestFunction: (DIGEST_FUNCTION
3232
return ret;
3333
}
3434

35+
- (NSString*) calcHashWithBytes: (NSArray *) subject withDigestFunction: (DIGEST_FUNCTION) digest withDigestLength: (int) digestLength {
36+
NSUInteger count = [subject count];
37+
unsigned char *array = (unsigned char *) malloc(count);
38+
39+
if (array == nil) return nil;
40+
41+
for (int i = 0; i < count; ++i) {
42+
array[i] = [[subject objectAtIndex: i] unsignedCharValue];
43+
}
44+
45+
unsigned char result[digestLength];
46+
47+
digest(array, count, result);
48+
49+
free(array);
50+
51+
NSMutableString *ret = [NSMutableString stringWithCapacity:digestLength * 2];
52+
for(int i = 0; i < digestLength; i++)
53+
{
54+
[ret appendFormat:@"%02x",result[i]];
55+
}
56+
57+
return ret;
58+
}
59+
3560
RCT_EXPORT_METHOD(sha1: (NSString *) data
3661
resolver: (RCTPromiseResolveBlock) resolve
3762
rejecter: (RCTPromiseRejectBlock) reject)
@@ -41,6 +66,20 @@ - (NSString*) calcHash: (NSString*) subject withDigestFunction: (DIGEST_FUNCTION
4166
resolve(ret);
4267
}
4368

69+
RCT_EXPORT_METHOD(sha1Bytes: (NSArray *) data
70+
resolver: (RCTPromiseResolveBlock) resolve
71+
rejecter: (RCTPromiseRejectBlock) reject)
72+
73+
{
74+
NSString *ret = [self calcHashWithBytes:data withDigestFunction:CC_SHA1 withDigestLength: CC_SHA1_DIGEST_LENGTH];
75+
76+
if (ret) {
77+
resolve(ret);
78+
} else {
79+
reject(@"error", @"out of memory", nil);
80+
}
81+
}
82+
4483
RCT_EXPORT_METHOD(sha256: (NSString *) data
4584
resolver: (RCTPromiseResolveBlock) resolve
4685
rejecter: (RCTPromiseRejectBlock) reject)
@@ -50,4 +89,18 @@ - (NSString*) calcHash: (NSString*) subject withDigestFunction: (DIGEST_FUNCTION
5089
resolve(ret);
5190
}
5291

92+
RCT_EXPORT_METHOD(sha256Bytes: (NSArray *) data
93+
resolver: (RCTPromiseResolveBlock) resolve
94+
rejecter: (RCTPromiseRejectBlock) reject)
95+
96+
{
97+
NSString *ret = [self calcHashWithBytes:data withDigestFunction:CC_SHA256 withDigestLength: CC_SHA256_DIGEST_LENGTH];
98+
99+
if (ret) {
100+
resolve(ret);
101+
} else {
102+
reject(@"error", @"out of memory", nil);
103+
}
104+
}
105+
53106
@end

sha256.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
declare module 'react-native-sha256' {
22
export const sha1: (input: string) => Promise<string>;
3+
export const sha1Bytes: (input: Array) => Promise<string>;
34
export const sha256: (input: string) => Promise<string>;
5+
export const sha256Bytes: (input: Array) => Promise<string>;
46
}

sha256.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ export function sha256(data: string) {
1212
return sha256Lib.sha256(data);
1313
}
1414

15+
export function sha256Bytes(data: Array) {
16+
return sha256Lib.sha256Bytes(data);
17+
}
18+
1519
export function sha1(data: string) {
1620
return sha256Lib.sha1(data);
1721
}
22+
23+
export function sha1Bytes(data: Array) {
24+
return sha256Lib.sha1Bytes(data);
25+
}

0 commit comments

Comments
 (0)