public byte[] sha256(byte[] message) { buffer = new byte[SodiumConstants.SHA256BYTES]; sodium().crypto_hash_sha256(buffer, message, message.length); return buffer; }
public byte[] randomBytes() { byte[] buffer = new byte[DEFAULT_SIZE]; sodium().randombytes(buffer, DEFAULT_SIZE); return buffer; } }
public byte[] randomBytes() { byte[] buffer = new byte[DEFAULT_SIZE]; sodium().randombytes(buffer, DEFAULT_SIZE); return buffer; } }
public byte[] sha512(byte[] message) { buffer = new byte[SodiumConstants.SHA512BYTES]; sodium().crypto_hash_sha512(buffer, message, message.length); return buffer; }
/** * Generate random bytes * * @param n number or random bytes * @return Byte array with random bytes */ public byte[] randomBytes(int n) { byte[] buffer = new byte[n]; sodium().randombytes(buffer, n); return buffer; }
public byte[] sha256(byte[] message) { buffer = new byte[SodiumConstants.SHA256BYTES]; sodium().crypto_hash_sha256(buffer, message, message.length); 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 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 byte[] sign(byte[] message) { byte[] signature = Util.prependZeros(SIGNATURE_BYTES, message); int[] bufferLen = new int[1]; sodium().crypto_sign_ed25519(signature, bufferLen, message, message.length, secretKey); signature = Util.slice(signature, 0, SIGNATURE_BYTES); return signature; }
public boolean verify(byte[] message, byte[] signature) { Util.checkLength(signature, SIGNATURE_BYTES); byte[] sigAndMsg = Util.merge(signature, message); byte[] buffer = Util.zeros(sigAndMsg.length); int[] bufferLen = new int[1]; return Util.isValid(sodium().crypto_sign_ed25519_open(buffer, bufferLen, sigAndMsg, sigAndMsg.length, key), "signature was forged or corrupted"); }
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[] 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[] encrypt(byte[] nonce, byte[] message) { checkLength(nonce, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); byte[] msg = Util.prependZeros(ZERO_BYTES, message); byte[] ct = Util.zeros(msg.length); isValid(sodium().crypto_secretbox_xsalsa20poly1305(ct, msg, msg.length, nonce, key), "Encryption failed"); return removeZeros(BOXZERO_BYTES, ct); }
public byte[] decrypt(byte[] nonce, byte[] ciphertext) { checkLength(nonce, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); byte[] ct = Util.prependZeros(BOXZERO_BYTES, ciphertext); byte[] message = Util.zeros(ct.length); isValid(sodium().crypto_secretbox_xsalsa20poly1305_open(message, ct, ct.length, nonce, key), "Decryption failed. Ciphertext failed verification"); return removeZeros(ZERO_BYTES, message); } }
public byte[] decrypt(byte[] nonce, byte[] ciphertext) { checkLength(nonce, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); byte[] ct = Util.prependZeros(BOXZERO_BYTES, ciphertext); byte[] message = Util.zeros(ct.length); isValid(sodium().crypto_secretbox_xsalsa20poly1305_open(message, ct, ct.length, nonce, key), "Decryption failed. Ciphertext failed verification"); return removeZeros(ZERO_BYTES, message); } }
@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)); } }
@Test public void ChaCha20Poly1305Keygen() { Sodium sodium= NaCl.sodium(); byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_keybytes()]; Sodium.crypto_aead_chacha20poly1305_keygen(key); assertEquals(key.length, Sodium.crypto_aead_chacha20poly1305_keybytes()); }
@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())); }