Skip to content

Commit 7cb1418

Browse files
authored
Merge pull request #53 from functionland/blockchain
Blockchain
2 parents 76c78fa + 0470d80 commit 7cb1418

File tree

19 files changed

+5320
-1451
lines changed

19 files changed

+5320
-1451
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const peerId //returns peerId as string
2323
bloxAddr: string, //leave empty for testing without a backend node
2424
exchange: 'noop'|'', //add noop for testing without a backend
2525
autoFlush: boolean, //Default to false. Always set to false unless you know what you are doing. explicitly write data to disk after each operation if set to true
26-
useRelay: boolean //default to true. If true it forces the connection through relay
26+
useRelay: boolean, //default to true. If true it forces the connection through relay
27+
refresh: boolean? //forces the fula object to be recreated. default is false
2728
)
2829
```
2930
@@ -41,7 +42,8 @@ await fula.init(
4142
bloxAddr: string, //leave empty for testing without a backend node
4243
exchange: 'noop'|'', //add noop for testing without a backend
4344
autoFlush: boolean, //Default to false. Always set to false unless you know what you are doing. explicitly write data to disk after each operation if set to true
44-
useRelay: boolean //default to true. If true it forces the connection through relay
45+
useRelay: boolean, //default to true. If true it forces the connection through relay
46+
refresh: boolean? //forces the fula object to be recreated. default is false
4547
);
4648
```
4749
@@ -131,7 +133,9 @@ await fula.isReady(
131133
//checks if client can reach server
132134
const result //returns true if it can, and false if it cannot
133135
=
134-
await fula.checkConnection();
136+
await fula.checkConnection(
137+
timeout: number? //default to 20. Maximum time in seconds that checkConnection waits before throwing an error
138+
);
135139

136140
```
137141
@@ -141,7 +145,19 @@ const result //returns true if there are, and false if everything is synced with
141145
=
142146
await fula.checkFailedActions(
143147
retry: boolean //if true, it tries to sync device with server, if not, it only checks
148+
timeout: number? //default to 20. Maximum time in seconds that checkConnection waits before throwing an error
149+
);
150+
```
151+
152+
```js
153+
//Gives access to the blox for a specific peerId. This call must be made from the authorizer only.
154+
const result //returns true if succesful and false if fails
155+
=
156+
await fula.setAuth(
157+
peerId: string, //peer ID of the app that needs access to the blox
158+
allow: boolean, // true to allow and false to remove access
144159
);
160+
145161
```
146162
147163
```js

android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ repositories {
6262
dependencies {
6363
//noinspection GradleDynamicVersion
6464
implementation "com.facebook.react:react-native:+" // From node_modules
65-
implementation 'com.github.functionland:fula-build-aar:v0.8.4' // From jitpack.io
65+
implementation 'com.github.functionland:fula-build-aar:1.11.2' // From jitpack.io
6666
implementation 'com.github.functionland:wnfs-build-aar:v1.4.1' // From jitpack.io
6767
implementation 'commons-io:commons-io:20030203.000550'
68+
implementation 'commons-codec:commons-codec:1.15'
6869
// implementation files('mobile.aar')
6970
}

android/src/main/java/land/fx/fula/Cryptography.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import android.util.Base64;
44

55
import java.io.UnsupportedEncodingException;
6+
import java.nio.charset.StandardCharsets;
67
import java.security.InvalidAlgorithmParameterException;
78
import java.security.InvalidKeyException;
89
import java.security.NoSuchAlgorithmException;
910
import java.security.spec.InvalidKeySpecException;
11+
import java.security.SecureRandom;
12+
import java.nio.ByteBuffer;
1013
import java.security.spec.InvalidParameterSpecException;
1114

1215
import javax.crypto.BadPaddingException;
@@ -17,24 +20,34 @@
1720
import javax.crypto.SecretKeyFactory;
1821
import javax.crypto.spec.PBEKeySpec;
1922
import javax.crypto.spec.SecretKeySpec;
23+
import javax.crypto.spec.GCMParameterSpec;
2024

2125
public class Cryptography {
2226
public static String encryptMsg(String message, SecretKey secret)
23-
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
24-
Cipher cipher = null;
25-
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
26-
cipher.init(Cipher.ENCRYPT_MODE, secret);
27-
byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
28-
return Base64.encodeToString(cipherText, Base64.NO_WRAP);
27+
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
28+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
29+
byte[] iv = new byte[12]; // Ensure this is randomly generated for each encryption.
30+
new SecureRandom().nextBytes(iv);
31+
GCMParameterSpec spec = new GCMParameterSpec(128, iv);
32+
cipher.init(Cipher.ENCRYPT_MODE, secret, spec);
33+
byte[] cipherText = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
34+
ByteBuffer byteBuffer = ByteBuffer.allocate(iv.length + cipherText.length);
35+
byteBuffer.put(iv);
36+
byteBuffer.put(cipherText);
37+
return Base64.encodeToString(byteBuffer.array(), Base64.NO_WRAP);
2938
}
3039

3140
public static String decryptMsg(String cipherText, SecretKey secret)
32-
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
33-
Cipher cipher = null;
34-
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
35-
cipher.init(Cipher.DECRYPT_MODE, secret);
36-
byte[] decode = Base64.decode(cipherText, Base64.NO_WRAP);
37-
String decryptString = new String(cipher.doFinal(decode), "UTF-8");
41+
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
42+
ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.decode(cipherText, Base64.NO_WRAP));
43+
byte[] iv = new byte[12];
44+
byteBuffer.get(iv);
45+
byte[] cipherBytes = new byte[byteBuffer.remaining()];
46+
byteBuffer.get(cipherBytes);
47+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
48+
GCMParameterSpec spec = new GCMParameterSpec(128, iv);
49+
cipher.init(Cipher.DECRYPT_MODE, secret, spec);
50+
String decryptString = new String(cipher.doFinal(cipherBytes), StandardCharsets.UTF_8);
3851
return decryptString;
3952
}
4053

0 commit comments

Comments
 (0)