-
Notifications
You must be signed in to change notification settings - Fork 82
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
868b42b
4dc2e93
8c6ebbd
970d009
809e320
b999518
cd6e07e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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) { | ||
this.publicKey = publicKey; | ||
this.privateKey = null; | ||
} | ||
|
||
public SealedBox(String publicKey, Encoder encoder) { | ||
this(encoder.decode(publicKey)); | ||
} | ||
|
||
public SealedBox(byte[] publicKey, byte[] privateKey) { | ||
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) { | ||
if (publicKey == null) | ||
throw new RuntimeException("Encryption failed. Public key not available."); | ||
byte[] ct = new byte[message.length + SEAL_BYTES]; | ||
isValid(sodium().crypto_box_seal( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Planning to remove the deprecated classes in a major release. |
||
ct, message, message.length, publicKey), | ||
"Encryption failed"); | ||
return ct; | ||
} | ||
|
||
public byte[] decrypt(byte[] ciphertext) { | ||
if (privateKey == null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to avoid waiting until decrypt time to determine that the public key passed at initialization is incorrect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed that as well. |
||
throw new RuntimeException("Decryption failed. Private key not available."); | ||
|
||
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to avoid waiting until encrypt time to determine that the public key passed at initialization is incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, moved that check into the constructor, now throwing
IllegalArgumentException
though that also inherits RuntimeException. Couldn't think of an appropriate checked exception here (if we want to go that way, we could do a custom exception though).