Skip to content

Commit 099a8d5

Browse files
committed
feat: Implement configurable hasher
1 parent d3e575b commit 099a8d5

File tree

5 files changed

+95
-26
lines changed

5 files changed

+95
-26
lines changed

README.md

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,49 @@ Compatibility Table
2020

2121
## Usage
2222

23-
`rawHash` is the hexadecimal representation
24-
25-
`encodedHash` is the string representation
26-
2723
```javascript
2824
import argon2 from 'react-native-argon2';
2925
const password = 'password';
3026
const salt = '1234567891011121314151617181920212223242526272829303132333435363';
31-
const result = await argon2(password, salt);
27+
const result = await argon2(password, salt, {});
3228
const { rawHash, encodedHash } = result;
3329
// rawHash: 031d6c82ddede1200f4794605052745dd562bd4db358e23dac1b11c052eff8d9
3430
// encodedHash: $argon2id$v=19$m=32768,t=2,p=1$MTIzNDU2Nzg5MTAxMTEyMTMxNDE1MTYxNzE4MTkyMDIxMjIyMzI0MjUyNjI3MjgyOTMwMzEzMjMzMzQzNTM2Mw$Ax1sgt3t4SAPR5RgUFJ0XdVivU2zWOI9rBsRwFLv+Nk
3531
```
32+
33+
### Input
34+
The package takes in the following variables:
35+
36+
| Parameter | Type |
37+
|---------------------|---------|
38+
| password | string |
39+
| salt | string |
40+
| config | object |
41+
| config.iterations | integer |
42+
| config.memory | integer |
43+
| config.parallelism | integer |
44+
| config.hashLength | integer |
45+
| config.mode | string |
46+
47+
You are not required to configure the third parameter which is the `config` object, however you do have to provide an empty object to it if you are not changing any of the values. You can set config values with the following example:
48+
49+
```javascript
50+
const result = await argon2(
51+
password,
52+
salt,
53+
{
54+
iterations: 5,
55+
memory: 16 * 1024,
56+
parallelism: 2,
57+
hashLength: 20,
58+
mode: 'argon2i'
59+
}
60+
);
61+
```
62+
63+
64+
### Output
65+
66+
`rawHash` is the hexadecimal representation
67+
68+
`encodedHash` is the string representation

android/src/main/java/com/poowf/argon2/RNArgon2Module.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.facebook.react.bridge.ReactContext;
88
import com.facebook.react.bridge.ReactContextBaseJavaModule;
99
import com.facebook.react.bridge.ReactMethod;
10+
import com.facebook.react.bridge.ReadableMap;
1011
import com.facebook.react.bridge.WritableMap;
1112
import com.facebook.react.bridge.WritableNativeMap;
1213
import com.facebook.react.bridge.Promise;
@@ -29,19 +30,21 @@ public String getName() {
2930
}
3031

3132
@ReactMethod
32-
public void argon2(String password, String salt, Promise promise) {
33+
public void argon2(String password, String salt, ReadableMap config, Promise promise) {
3334
try {
3435
final byte[] passwordBytes = password.getBytes("UTF-8");
3536
final byte[] saltBytes = salt.getBytes("UTF-8");
36-
Integer iterations = new Integer(2);
37-
Integer memory = new Integer(32 * 1024);
38-
Integer parallelism = new Integer(1);
39-
Integer hashLength = new Integer(32);
37+
38+
Integer iterations = config.hasKey("iterations") ? new Integer(config.getInt("iterations")) : new Integer(2);
39+
Integer memory = config.hasKey("memory") ? new Integer(config.getInt("memory")) : new Integer(32 * 1024);
40+
Integer parallelism = config.hasKey("parallelism") ? new Integer(config.getInt("parallelism")) : new Integer(1);
41+
Integer hashLength = config.hasKey("hashLength") ? new Integer(config.getInt("hashLength")) : new Integer(32);
42+
Argon2Mode mode = config.hasKey("mode") ? getArgon2Mode(config.getString("mode")) : Argon2Mode.ARGON2_ID;
4043

4144
final Argon2Kt argon2Kt = new Argon2Kt();
4245

4346
final Argon2KtResult hashResult = argon2Kt.hash(
44-
Argon2Mode.ARGON2_ID,
47+
mode,
4548
passwordBytes,
4649
saltBytes,
4750
iterations,
@@ -60,4 +63,24 @@ public void argon2(String password, String salt, Promise promise) {
6063
promise.reject("Failed to generate argon2 hash", exception);
6164
}
6265
}
66+
67+
public Argon2Mode getArgon2Mode(String mode) {
68+
Argon2Mode selectedMode;
69+
switch (mode) {
70+
case "argon2d":
71+
selectedMode = Argon2Mode.ARGON2_D;
72+
break;
73+
case "argon2i":
74+
selectedMode = Argon2Mode.ARGON2_I;
75+
break;
76+
case "argon2id":
77+
selectedMode = Argon2Mode.ARGON2_ID;
78+
break;
79+
default:
80+
selectedMode = Argon2Mode.ARGON2_ID;
81+
break;
82+
}
83+
84+
return selectedMode;
85+
}
6386
}

android/src/main/java/com/poowf/argon2/RNArgon2Package.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.List;
1212

1313
public class RNArgon2Package implements ReactPackage {
14-
1514
@Override
1615
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
1716
List<NativeModule> modules = new ArrayList<>();
@@ -20,14 +19,8 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
2019
return modules;
2120
}
2221

23-
// Deprecated since React Native 0.47
24-
public List<Class<? extends JavaScriptModule>> createJSModules() {
25-
return Collections.emptyList();
26-
}
27-
2822
@Override
2923
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
3024
return Collections.emptyList();
3125
}
32-
3326
}

ios/RNArgon2.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import CatCrypto
33

44
@objc(RNArgon2)
55
class RNArgon2: NSObject {
6-
76
@objc
87
static func requiresMainQueueSetup() -> Bool {
98
return true
109
}
1110

1211
@objc
13-
func argon2(_ password: String, salt: String, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
12+
func argon2(_ password: String, salt: String, config: NSDictionary? = nil, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
1413
let argon2Context = CatArgon2Context.init();
15-
argon2Context.iterations = 2;
16-
argon2Context.memory = 32 * 1024;
17-
argon2Context.parallelism = 1;
14+
let configDict = config as! Dictionary<String,Any>
15+
16+
argon2Context.iterations = configDict["iterations", default: 2 ] as! Int;
17+
argon2Context.memory = configDict["memory", default: 32 * 1024 ] as! Int;
18+
argon2Context.parallelism = configDict["parallelism", default: 1 ] as! Int;
1819
argon2Context.salt = salt;
19-
argon2Context.hashLength = 32;
20-
argon2Context.mode = .argon2id;
20+
argon2Context.hashLength = configDict["hashLength", default: 32 ] as! Int;
21+
argon2Context.mode = getArgon2Mode(mode: configDict["mode", default: "argon2id" ] as! String);
2122

2223
let argon2Crypto = CatArgon2Crypto.init(context: argon2Context);
2324
let encodedResult = argon2Crypto.hash(password: password);
@@ -41,4 +42,23 @@ class RNArgon2: NSObject {
4142
resolve(resultDictionary);
4243
}
4344

45+
func getArgon2Mode(mode: String) -> CatArgon2Mode {
46+
var selectedMode: CatArgon2Mode;
47+
switch mode {
48+
case "argon2d":
49+
selectedMode = CatArgon2Mode.argon2d;
50+
break;
51+
case "argon2i":
52+
selectedMode = CatArgon2Mode.argon2i;
53+
break;
54+
case "argon2id":
55+
selectedMode = CatArgon2Mode.argon2id;
56+
break;
57+
default:
58+
selectedMode = CatArgon2Mode.argon2id;
59+
break;
60+
}
61+
62+
return selectedMode;
63+
}
4464
}

ios/RNArgon2Bridge.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
@interface RCT_EXTERN_MODULE(RNArgon2, NSObject)
44

5-
RCT_EXTERN_METHOD(argon2: (NSString *)password salt:(NSString *)salt resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
5+
RCT_EXTERN_METHOD(argon2: (NSString *)password salt:(NSString *)salt config:(NSDictionary *)config resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
66

77
@end

0 commit comments

Comments
 (0)