-
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
Open
om26er
wants to merge
7
commits into
joshjdevl:master
Choose a base branch
from
om26er:implement-sealed-box
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
868b42b
Add preliminary (untested) SealedBox support
om26er 4dc2e93
use the sodium() method which ensures thins are initialized
om26er 8c6ebbd
Ensure public and private keys are not null when constructing
om26er 970d009
No need to add unchecked exception to the signature
om26er 809e320
Merge branch 'master' into implement-sealed-box
om26er b999518
Make decrypt a static method
om26er cd6e07e
Better error messaging
om26er File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 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 ?
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.
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.