Skip to content

Commit

Permalink
remove dependency of libsodium
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed Feb 26, 2024
1 parent 2ce33d2 commit c6f45a7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
7 changes: 2 additions & 5 deletions autobahn/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ dependencies {
implementation 'org.web3j:core:5.0.0'
implementation 'org.web3j:abi:5.0.0'
implementation 'org.web3j:utils:5.0.0'
if (IS_ANDROID) {
implementation 'com.github.joshjdevl.libsodiumjni:libsodium-jni-aar:2.0.2'
} else {
implementation 'com.github.joshjdevl.libsodiumjni:libsodium-jni:2.0.2'
if (!IS_ANDROID) {
implementation 'org.json:json:20240205'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.5'
}
Expand Down Expand Up @@ -188,7 +185,7 @@ afterEvaluate {
artifactId ARTIFACT_ANDROID
} else {
from components.java
artifactId IS_NEXT ? ARTIFACT_NEXT: ARTIFACT_JAVA
artifactId IS_NEXT ? ARTIFACT_NEXT : ARTIFACT_JAVA
}

artifact sourcesJar
Expand Down
17 changes: 3 additions & 14 deletions autobahn/src/main/java/xbr/network/crypto/SealedBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.math.ec.rfc7748.X25519;
import org.bouncycastle.util.Arrays;
import org.libsodium.jni.encoders.Encoder;

import static org.libsodium.jni.SodiumConstants.NONCE_BYTES;
import static org.libsodium.jni.SodiumConstants.PUBLICKEY_BYTES;
import static org.libsodium.jni.SodiumConstants.SECRETKEY_BYTES;

import io.crossbar.autobahn.utils.Pair;
import xbr.network.Util;

public class SealedBox {

private static final int MAC_BYTES = 16;
private static int PUBLICKEY_BYTES = 32;
private static final int SEAL_BYTES = PUBLICKEY_BYTES + MAC_BYTES;

private byte[] publicKey;
Expand All @@ -32,9 +28,6 @@ public SealedBox(byte[] publicKey) {
this.privateKey = null;
}

public SealedBox(String publicKey, Encoder encoder) {
this(encoder.decode(publicKey));
}

public SealedBox(byte[] publicKey, byte[] privateKey) {
if (publicKey == null) {
Expand All @@ -47,10 +40,6 @@ public SealedBox(byte[] publicKey, byte[] privateKey) {
this.privateKey = privateKey;
}

public SealedBox(String publicKey, String privateKey, Encoder encoder) {
this(encoder.decode(publicKey), encoder.decode(privateKey));
}

public byte[] encrypt(byte[] message) {
Pair<byte[], byte[]> keyPair = Util.generateX25519KeyPair();
byte[] nonce = createNonce(keyPair.first, publicKey);
Expand All @@ -60,7 +49,7 @@ public byte[] encrypt(byte[] message) {
ParametersWithIV params = new ParametersWithIV(new KeyParameter(sharedSecret), nonce);
cipher.init(true, params);

byte[] sk = new byte[SECRETKEY_BYTES];
byte[] sk = new byte[Util.SECRET_KEY_LEN];
cipher.processBytes(sk, 0, sk.length, sk, 0);

// encrypt the message
Expand All @@ -78,7 +67,7 @@ public byte[] encrypt(byte[] message) {
}

private byte[] createNonce(byte[] ephemeralPublicKey, byte[] recipientPublicKey) {
Blake2bDigest blake2b = new Blake2bDigest(NONCE_BYTES * 8);
Blake2bDigest blake2b = new Blake2bDigest(Util.NONCE_SIZE * 8);
byte[] nonce = new byte[blake2b.getDigestSize()];

blake2b.update(ephemeralPublicKey, 0, ephemeralPublicKey.length);
Expand Down

0 comments on commit c6f45a7

Please sign in to comment.