55
66import java .io .UnsupportedEncodingException ;
77import java .security .SecureRandom ;
8+ import java .util .Base64 ;
9+ import java .lang .System ;
810import 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}
0 commit comments