public byte[] randomBytes() { byte[] buffer = new byte[DEFAULT_SIZE]; sodium().randombytes(buffer, DEFAULT_SIZE); return buffer; } }
public byte[] blake2(byte[] message) throws UnsupportedOperationException { if (!blakeSupportedVersion()) throw new UnsupportedOperationException(); buffer = new byte[SodiumConstants.BLAKE2B_OUTBYTES]; sodium().crypto_generichash_blake2b(buffer, SodiumConstants.BLAKE2B_OUTBYTES, message, message.length, new byte[0], 0); return buffer; }
public byte[] sha256(byte[] message) { buffer = new byte[SodiumConstants.SHA256BYTES]; sodium().crypto_hash_sha256(buffer, message, message.length); return buffer; }
int ret=0; long publickeylen=Sodium.crypto_sign_publickeybytes(); long privatekeylen=Sodium.crypto_sign_secretkeybytes(); System.out.println("PublicKeyLen="+publickeylen); System.out.println("PrivateKeyLen="+privatekeylen); final byte[] private_key=new byte[(int)privatekeylen]; System.out.println("Generating keypair"); Sodium.randombytes(public_key,(int)publickeylen); Sodium.randombytes(private_key,(int)privatekeylen); ret=Sodium.crypto_sign_keypair(public_key,private_key); System.out.println(ret); System.out.println("Generated keypair"); long signaturelen=Sodium.crypto_sign_bytes(); byte[] signed_message=new byte[(int)signaturelen+originalmessage.length]; final int[] signed_message_len = new int[1]; System.out.println("Signing message"); ret=Sodium.crypto_sign(signed_message,signed_message_len,originalmessage,originalmessage.length,private_key); System.out.println(ret); System.out.println("byte length="+signed_message.length); final int[] messageSize=new int[1]; System.out.println("Verifying message"); ret=Sodium.crypto_sign_open(message, messageSize, signed_message, signed_message_len[0], public_key); System.out.println(ret); System.out.println("Recovered message="+new String(message));
@Test public void testGenericHashInit() { Sodium sodium= NaCl.sodium(); byte[] state = new byte[Sodium.crypto_generichash_statebytes()]; byte[] hash = new byte[Sodium.crypto_generichash_bytes()]; byte[] key = new byte[Sodium.crypto_generichash_keybytes()]; Sodium.randombytes(key, key.length); assertEquals(0,Sodium.crypto_generichash_init(state, key, key.length, hash.length)); byte[] message="message".getBytes(); assertEquals(0,Sodium.crypto_generichash_update(state,message,message.length)); assertEquals(0,Sodium.crypto_generichash_final(state,hash,Sodium.crypto_generichash_bytes())); }
@Test public void testGenericHash() { Sodium sodium= NaCl.sodium(); byte[] hash = new byte[Sodium.crypto_generichash_bytes()]; byte[] message = "message".getBytes(); byte[] key = new byte[Sodium.crypto_generichash_keybytes()]; Sodium.randombytes(key, key.length); assertEquals(0,Sodium.crypto_generichash(hash,Sodium.crypto_generichash_bytes(),message,message.length,key,key.length)); } }
public KeyPair() { this.secretKey = zeros(SECRETKEY_BYTES); this.publicKey = zeros(PUBLICKEY_BYTES); sodium().crypto_box_curve25519xsalsa20poly1305_keypair(publicKey, secretKey); }
public Point mult(byte[] n) { byte[] result = zeros(SCALAR_BYTES); sodium().crypto_scalarmult_curve25519(result, n, point); return new Point(result); }
public KeyPair(byte[] seed){ Util.checkLength(seed, SECRETKEY_BYTES); this.seed = seed; this.secretKey = zeros(SECRETKEY_BYTES); this.publicKey = zeros(PUBLICKEY_BYTES); Util.isValid(sodium().crypto_box_curve25519xsalsa20poly1305_seed_keypair(publicKey, secretKey, seed), "Failed to generate a key pair"); }
public byte[] encrypt(byte[] nonce, byte[] message) { checkLength(nonce, NONCE_BYTES); byte[] msg = prependZeros(ZERO_BYTES, message); byte[] ct = new byte[msg.length]; isValid(sodium().crypto_box_curve25519xsalsa20poly1305(ct, msg, msg.length, nonce, publicKey, privateKey), "Encryption failed"); return removeZeros(BOXZERO_BYTES, ct); }
public byte[] decrypt(byte[] nonce, byte[] ciphertext) { checkLength(nonce, NONCE_BYTES); byte[] ct = prependZeros(BOXZERO_BYTES, ciphertext); byte[] message = new byte[ct.length]; isValid(sodium().crypto_box_curve25519xsalsa20poly1305_open(message, ct, message.length, nonce, publicKey, privateKey), "Decryption failed. Ciphertext failed verification."); return removeZeros(ZERO_BYTES, message); }
public byte[] sha512(byte[] message) { buffer = new byte[SodiumConstants.SHA512BYTES]; sodium().crypto_hash_sha512(buffer, message, message.length); return buffer; }
public String pwhash_scryptsalsa208sha256(String passwd, Encoder encoder, byte[] salt, int opslimit, int memlimit) { buffer = new byte[KEY_LEN]; //sodium().crypto_pwhash(buffer, buffer.length, passwd.getBytes(), passwd.length(), salt, opslimit, memlimit,sodium().crypto_pwhash_alg_default()); sodium().crypto_pwhash_scryptsalsa208sha256(buffer, buffer.length, passwd.getBytes(), passwd.length(), salt, opslimit, memlimit); return encoder.encode(buffer); }
int ret=0; long publickeylen=Sodium.crypto_sign_publickeybytes(); long privatekeylen=Sodium.crypto_sign_secretkeybytes(); System.out.println("PublicKeyLen="+publickeylen); System.out.println("PrivateKeyLen="+privatekeylen); final byte[] private_key=new byte[(int)privatekeylen]; System.out.println("Generating keypair"); Sodium.randombytes(public_key,(int)publickeylen); Sodium.randombytes(private_key,(int)privatekeylen); ret=Sodium.crypto_sign_keypair(public_key,private_key); System.out.println(ret); System.out.println("Generated keypair"); long signaturelen=Sodium.crypto_sign_bytes(); byte[] signed_message=new byte[(int)signaturelen+originalmessage.length]; final int[] signed_message_len = new int[1]; System.out.println("Signing message"); ret=Sodium.crypto_sign(signed_message,signed_message_len,originalmessage,originalmessage.length,private_key); System.out.println(ret); System.out.println("byte length="+signed_message.length); final int[] messageSize=new int[1]; System.out.println("Verifying message"); ret=Sodium.crypto_sign_open(message, messageSize, signed_message, signed_message_len[0], public_key_fromfile); System.out.println(ret); System.out.println("Recovered message="+new String(message));
public KeyPair() { this.secretKey = zeros(SECRETKEY_BYTES); this.publicKey = zeros(PUBLICKEY_BYTES); sodium().crypto_box_curve25519xsalsa20poly1305_keypair(publicKey, secretKey); }
public Point mult(byte[] n) { byte[] result = zeros(SCALAR_BYTES); sodium().crypto_scalarmult_curve25519(result, n, point); return new Point(result); }
public KeyPair(byte[] seed){ Util.checkLength(seed, SECRETKEY_BYTES); this.seed = seed; this.secretKey = zeros(SECRETKEY_BYTES); this.publicKey = zeros(PUBLICKEY_BYTES); Util.isValid(sodium().crypto_box_curve25519xsalsa20poly1305_seed_keypair(publicKey, secretKey, seed), "Failed to generate a key pair"); }
public byte[] encrypt(byte[] nonce, byte[] message) { checkLength(nonce, NONCE_BYTES); byte[] msg = prependZeros(ZERO_BYTES, message); byte[] ct = new byte[msg.length]; isValid(sodium().crypto_box_curve25519xsalsa20poly1305(ct, msg, msg.length, nonce, publicKey, privateKey), "Encryption failed"); return removeZeros(BOXZERO_BYTES, ct); }
public byte[] decrypt(byte[] nonce, byte[] ciphertext) { checkLength(nonce, NONCE_BYTES); byte[] ct = prependZeros(BOXZERO_BYTES, ciphertext); byte[] message = new byte[ct.length]; isValid(sodium().crypto_box_curve25519xsalsa20poly1305_open(message, ct, message.length, nonce, publicKey, privateKey), "Decryption failed. Ciphertext failed verification."); return removeZeros(ZERO_BYTES, message); }
public byte[] sha512(byte[] message) { buffer = new byte[SodiumConstants.SHA512BYTES]; sodium().crypto_hash_sha512(buffer, message, message.length); return buffer; }