Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SealedBox support #127

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/main/java/org/libsodium/jni/crypto/SealedBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.libsodium.jni.crypto;

import org.libsodium.jni.encoders.Encoder;

import static org.libsodium.jni.NaCl.sodium;
import static org.libsodium.jni.SodiumConstants.SEAL_BYTES;
import static org.libsodium.jni.crypto.Util.isValid;

public class SealedBox {

private byte[] publicKey;
private byte[] privateKey;

public SealedBox(byte[] publicKey) {
if (publicKey == null) {
throw new IllegalArgumentException("Public key must not be null");
}
this.publicKey = publicKey;
this.privateKey = null;
}

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

public SealedBox(byte[] publicKey, byte[] privateKey) {
if (publicKey == null) {
throw new IllegalArgumentException("Public key must not be null");
}
if (privateKey == null) {
throw new IllegalArgumentException("Private key must not be null");
}
this.publicKey = publicKey;
this.privateKey = privateKey;
}

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

public byte[] encrypt(byte[] message) {
byte[] ct = new byte[message.length + SEAL_BYTES];
isValid(sodium().crypto_box_seal(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Util class is marked deprecated and should not be used.

isValid is supposed to return a boolean, though throws a RuntimeException.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have removed the usage of isValid and have moved the check internally.

What's the planned approach to deal with all the deprecated classes ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Planning to remove the deprecated classes in a major release.
The deprecated classes haven't been removed yet as it's unclear impact to downstream developers.

ct, message, message.length, publicKey),
"Encryption failed");
return ct;
}

public byte[] decrypt(byte[] ciphertext) {
byte[] message = new byte[ciphertext.length - SEAL_BYTES];
isValid(sodium().crypto_box_seal_open(
message, ciphertext, ciphertext.length, publicKey, privateKey),
"Decryption failed. Ciphertext failed verification");
return message;
}
}