-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SecretBox: refactor box and boxOpen to accept nonce directly (#18)
* refactor box and boxOpen to accept nonce parameter directly * add functions to prepend and extract nonce * move generateRandomBytesArray() to util and make it public * remove functions to prepend and extract nonce
- Loading branch information
1 parent
8e5e56f
commit 15cd56f
Showing
6 changed files
with
68 additions
and
73 deletions.
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
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 |
---|---|---|
@@ -1,56 +1,39 @@ | ||
package io.xconn.cryptology; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Arrays; | ||
|
||
import static io.xconn.cryptology.Util.MAC_SIZE; | ||
import static io.xconn.cryptology.Util.NONCE_SIZE; | ||
import static io.xconn.cryptology.Util.SECRET_KEY_LEN; | ||
|
||
public class SecretBox { | ||
|
||
public static byte[] box(byte[] message, byte[] privateKey) { | ||
byte[] output = new byte[message.length + MAC_SIZE + NONCE_SIZE]; | ||
box(output, message, privateKey); | ||
public static byte[] box(byte[] nonce, byte[] message, byte[] privateKey) { | ||
byte[] output = new byte[message.length + MAC_SIZE]; | ||
box(output, nonce, message, privateKey); | ||
|
||
return output; | ||
} | ||
|
||
public static void box(byte[] output, byte[] message, byte[] privateKey) { | ||
byte[] nonce = generateNonce(); | ||
byte[] cipherWithoutNonce = Util.encrypt(nonce, message, privateKey); | ||
|
||
System.arraycopy(nonce, 0, output, 0, nonce.length); | ||
System.arraycopy(cipherWithoutNonce, 0, output, nonce.length, cipherWithoutNonce.length); | ||
public static void box(byte[] output, byte[] nonce, byte[] message, byte[] privateKey) { | ||
Util.encrypt(output, nonce, message, privateKey); | ||
} | ||
|
||
|
||
public static byte[] boxOpen(byte[] ciphertext, byte[] privateKey) { | ||
byte[] plainText = new byte[ciphertext.length - MAC_SIZE - NONCE_SIZE]; | ||
boxOpen(plainText, ciphertext, privateKey); | ||
public static byte[] boxOpen(byte[] nonce, byte[] ciphertext, byte[] privateKey) { | ||
byte[] plainText = new byte[ciphertext.length - MAC_SIZE]; | ||
boxOpen(plainText, nonce, ciphertext, privateKey); | ||
|
||
return plainText; | ||
} | ||
|
||
public static void boxOpen(byte[] output, byte[] ciphertext, byte[] privateKey) { | ||
byte[] nonce = Arrays.copyOfRange(ciphertext, 0, NONCE_SIZE); | ||
byte[] message = Arrays.copyOfRange(ciphertext, NONCE_SIZE, ciphertext.length); | ||
|
||
Util.decrypt(output, nonce, message, privateKey); | ||
public static void boxOpen(byte[] output, byte[] nonce, byte[] ciphertext, byte[] privateKey) { | ||
Util.decrypt(output, nonce, ciphertext, privateKey); | ||
} | ||
|
||
public static byte[] generateSecret() { | ||
return generateRandomBytesArray(SECRET_KEY_LEN); | ||
} | ||
|
||
static byte[] generateNonce() { | ||
return generateRandomBytesArray(NONCE_SIZE); | ||
return Util.generateRandomBytesArray(SECRET_KEY_LEN); | ||
} | ||
|
||
static byte[] generateRandomBytesArray(int size) { | ||
byte[] randomBytes = new byte[size]; | ||
SecureRandom random = new SecureRandom(); | ||
random.nextBytes(randomBytes); | ||
return randomBytes; | ||
public static byte[] generateNonce() { | ||
return Util.generateRandomBytesArray(NONCE_SIZE); | ||
} | ||
} |
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
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
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
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