Skip to content

Commit de6b12c

Browse files
committed
TweetNaclFast.java :
* modified and added randombytes() methods to return the random byte array with convenience randombytes(int len) and randombytes(byte[] b). * added makeBoxNonce() and makeSecretBoxNonce() to make it easier to generate a correct nonce value (even though those nonce sizes happen to be the same... * added base64EncodeToString(), base64Decode(), hexEncodeToString(), hexDecode(), to facilitate the common encoding formats for byte[]'s * added a few tests for the newly defined methods
1 parent 816defa commit de6b12c

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

src/com/iwebpp/crypto/TweetNaclFast.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import java.io.UnsupportedEncodingException;
77
import java.security.SecureRandom;
8+
import java.util.Base64;
9+
import java.lang.System;
810
import java.util.concurrent.atomic.AtomicLong;
911

1012

@@ -3313,7 +3315,23 @@ public static int crypto_sign_open(byte [] m, long dummy /* *mlen not used*/, by
33133315
* */
33143316
private static final SecureRandom jrandom = new SecureRandom();
33153317

3316-
public static void randombytes(byte [] x, int len) {
3318+
public static byte[] randombytes(byte [] x) {
3319+
jrandom.nextBytes(x);
3320+
return x;
3321+
}
3322+
3323+
public static byte[] randombytes(int len) {
3324+
return randombytes(new byte[len]);
3325+
}
3326+
3327+
public static byte[] randombytes(byte [] x, int len) {
3328+
byte [] b = randombytes(len);
3329+
System.arraycopy(b, 0, x, 0, len);
3330+
return x;
3331+
}
3332+
3333+
/*
3334+
public static byte[] randombytes(byte [] x, int len) {
33173335
int ret = len % 8;
33183336
long rnd;
33193337
@@ -3335,6 +3353,47 @@ public static void randombytes(byte [] x, int len) {
33353353
for (int i = len-ret; i < len; i ++)
33363354
x[i] = (byte) (rnd >>> 8*i);
33373355
}
3356+
return x;
3357+
}
3358+
*/
3359+
3360+
public static byte[] makeBoxNonce() {
3361+
return randombytes(Box.nonceLength);
33383362
}
3363+
3364+
public static byte[] makeSecretBoxNonce() {
3365+
return randombytes(SecretBox.nonceLength);
3366+
}
3367+
3368+
public static String base64EncodeToString(byte [] b) {
3369+
return Base64.getUrlEncoder().withoutPadding().encodeToString(b);
3370+
}
3371+
// byte[] Base64.getUrlEncoder().withoutPadding().encode(b);
3372+
3373+
public static byte[] base64Decode(String s) {
3374+
return Base64.getUrlDecoder().decode(s);
3375+
}
3376+
// byte[] Base64.getUrlDecoder().decode(byte[] b)
3377+
3378+
public static String hexEncodeToString( byte [] raw ) {
3379+
String HEXES = "0123456789ABCDEF";
3380+
final StringBuilder hex = new StringBuilder( 2 * raw.length );
3381+
for ( final byte b : raw ) {
3382+
hex.append(HEXES.charAt((b & 0xF0) >> 4))
3383+
.append(HEXES.charAt((b & 0x0F)));
3384+
}
3385+
return hex.toString();
3386+
}
3387+
3388+
public static byte[] hexDecode(String s) {
3389+
byte[] b = new byte[s.length() / 2];
3390+
for (int i = 0; i < s.length(); i += 2) {
3391+
b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
3392+
+ Character.digit(s.charAt(i+1), 16));
3393+
}
3394+
return b;
3395+
}
33393396

3397+
// public static boolean java.util.Arrays.equals(array1, array2);
3398+
33403399
}

src/com/iwebpp/crypto/tests/TweetNaclFastTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.iwebpp.crypto.tests;
22

33
import java.io.UnsupportedEncodingException;
4+
import java.util.Arrays;
45

56
import com.iwebpp.crypto.TweetNaclFast;
6-
import static com.iwebpp.crypto.TweetNaclFast.Box.nonceLength;
7-
87

98
public final class TweetNaclFastTest {
109
private static final String TAG = "TweetNaclFastTest";
11-
10+
1211
private boolean testBox() throws UnsupportedEncodingException {
1312
// keypair A
1413
byte [] ska = new byte[32]; for (int i = 0; i < 32; i ++) ska[i] = 0;
@@ -19,6 +18,7 @@ private boolean testBox() throws UnsupportedEncodingException {
1918
skat += " "+ka.getSecretKey()[i];
2019
Log.d(TAG, "skat: "+skat);
2120

21+
2222
String pkat = "";
2323
for (int i = 0; i < ka.getPublicKey().length; i ++)
2424
pkat += " "+ka.getPublicKey()[i];
@@ -96,13 +96,21 @@ private boolean testBox() throws UnsupportedEncodingException {
9696
private boolean testBoxNonce() throws UnsupportedEncodingException {
9797

9898
// explicit nonce
99-
byte [] theNonce = new byte[nonceLength];
100-
com.iwebpp.crypto.TweetNaclFast.randombytes(theNonce, nonceLength);
99+
byte [] theNonce = TweetNaclFast.makeBoxNonce();
100+
byte [] theNonce2 = TweetNaclFast.base64Decode(
101+
TweetNaclFast.base64EncodeToString(theNonce));
102+
Log.d(TAG, "BoxNonce Base64 test Equal: " + "\"" + java.util.Arrays.equals(theNonce, theNonce2) + "\"");
103+
byte [] theNonce3 = TweetNaclFast.hexDecode(
104+
TweetNaclFast.hexEncodeToString(theNonce));
105+
Log.d(TAG, "BoxNonce Hex test Equal: " + "\"" + java.util.Arrays.equals(theNonce, theNonce3) + "\"");
101106
String theNoncet = "";
102107
for (int i = 0; i < theNonce.length; i ++)
103108
theNoncet += " "+theNonce[i];
104109
Log.d(TAG, "BoxNonce: "+theNoncet);
105-
110+
Log.d(TAG, "BoxNonce: " + "\"" + TweetNaclFast.base64EncodeToString(theNonce) + "\"");
111+
Log.d(TAG, "BoxNonce: " + "\"" + TweetNaclFast.hexEncodeToString(theNonce) + "\"");
112+
113+
106114

107115
// keypair A
108116
byte [] ska = new byte[32]; for (int i = 0; i < 32; i ++) ska[i] = 0;
@@ -250,8 +258,7 @@ private boolean testSecretBox() throws UnsupportedEncodingException {
250258
private boolean testSecretBoxNonce() throws UnsupportedEncodingException {
251259

252260
// explicit nonce
253-
byte [] theNonce = new byte[nonceLength];
254-
com.iwebpp.crypto.TweetNaclFast.randombytes(theNonce, nonceLength);
261+
byte [] theNonce = TweetNaclFast.makeSecretBoxNonce();
255262
String theNoncet = "";
256263
for (int i = 0; i < theNonce.length; i ++)
257264
theNoncet += " "+theNonce[i];

0 commit comments

Comments
 (0)