Java源码示例:org.libsodium.jni.NaCl
示例1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// load libsodium for JNI access
this.sodium = NaCl.sodium();
Typeface type = Typeface.createFromAsset(getAssets(), "rounds_black.otf");
((TextView) findViewById(R.id.splashText)).setTypeface(type);
// start MainService and call back via onServiceConnected()
MainService.start(this);
bindService(new Intent(this, MainService.class), this, Service.BIND_AUTO_CREATE);
}
示例2
@Test
public void testSealedBox() {
Sodium sodium= NaCl.sodium();
int ret=0;
long publickeylen=Sodium.crypto_box_publickeybytes();
long privatekeylen=Sodium.crypto_box_secretkeybytes();
final byte[] public_key=new byte[(int)publickeylen];
final byte[] private_key=new byte[(int)privatekeylen];
Sodium.crypto_box_keypair(public_key,private_key);
String message="testmessage";
byte[] ciphertext=new byte[Sodium.crypto_box_sealbytes()+message.length()];
Sodium.crypto_box_seal(ciphertext,message.getBytes(),message.length(),public_key);
byte[] plaintext=new byte[message.length()];
ret=Sodium.crypto_box_seal_open(plaintext,ciphertext,ciphertext.length,public_key,private_key);
Assert.assertEquals(0,ret);
}
示例3
@Test
public void testGenericHashInit() {
Sodium sodium= NaCl.sodium();
String Password = "hunter2";
byte[] key = new byte[Sodium.crypto_box_seedbytes()];
byte[] passwd = Password.getBytes();
byte[] salt = new byte[]{ 88, (byte)240, (byte)185, 66, (byte)195, 101, (byte)160, (byte)138, (byte)137, 78, 1, 2, 3, 4, 5, 6};
Sodium.crypto_pwhash(
key,
key.length,
passwd,
passwd.length,
salt,
Sodium.crypto_pwhash_opslimit_interactive(),
Sodium.crypto_pwhash_memlimit_interactive(),
Sodium.crypto_pwhash_alg_default()
);
//assertEquals(key[0], (byte)49);
}
示例4
@Test
public void testSealedBox() {
Sodium sodium= NaCl.sodium();
int ret=0;
long publickeylen=Sodium.crypto_box_publickeybytes();
long privatekeylen=Sodium.crypto_box_secretkeybytes();
final byte[] public_key=new byte[(int)publickeylen];
final byte[] private_key=new byte[(int)privatekeylen];
Sodium.crypto_box_keypair(public_key,private_key);
String message="testmessage";
byte[] ciphertext=new byte[Sodium.crypto_box_sealbytes()+message.length()];
Sodium.crypto_box_seal(ciphertext,message.getBytes(),message.length(),public_key);
byte[] plaintext=new byte[message.length()];
ret=Sodium.crypto_box_seal_open(plaintext,ciphertext,ciphertext.length,public_key,private_key);
Assert.assertEquals(0,ret);
}
示例5
@Test
public void testGenericHashInit() {
Sodium sodium= NaCl.sodium();
String Password = "hunter2";
byte[] key = new byte[Sodium.crypto_box_seedbytes()];
byte[] passwd = Password.getBytes();
byte[] salt = new byte[]{ 88, (byte)240, (byte)185, 66, (byte)195, 101, (byte)160, (byte)138, (byte)137, 78, 1, 2, 3, 4, 5, 6};
Sodium.crypto_pwhash(
key,
key.length,
passwd,
passwd.length,
salt,
Sodium.crypto_pwhash_opslimit_interactive(),
Sodium.crypto_pwhash_memlimit_interactive(),
Sodium.crypto_pwhash_alg_default()
);
//assertEquals(key[0], (byte)49);
}
示例6
/**
* Convert a wallet seed to private key
*
* @param seed Wallet seed
* @return private key
*/
public static String seedToPrivate(String seed) {
Sodium sodium = NaCl.sodium();
byte[] state = new byte[Sodium.crypto_generichash_statebytes()];
byte[] key = new byte[Sodium.crypto_generichash_keybytes()];
byte[] seed_b = NanoUtil.hexToBytes(seed);
byte[] index_b = {0x00, 0x00, 0x00, 0x00};
byte[] output = new byte[32];
Sodium.crypto_generichash_blake2b_init(state, key, 0, 32);
Sodium.crypto_generichash_blake2b_update(state, seed_b, seed_b.length);
Sodium.crypto_generichash_blake2b_update(state, index_b, index_b.length);
Sodium.crypto_generichash_blake2b_final(state, output, output.length);
return bytesToHex(output);
}
示例7
/**
* Compute hash to use to generate an open work block
*
* @param source Source address
* @param representative Representative address
* @param account Account address
* @return Open Hash
*/
public static String computeOpenHash(String source, String representative, String account) {
Sodium sodium = NaCl.sodium();
byte[] state = new byte[Sodium.crypto_generichash_statebytes()];
byte[] key = new byte[Sodium.crypto_generichash_keybytes()];
byte[] source_b = hexToBytes(source);
byte[] representative_b = hexToBytes(representative);
byte[] account_b = hexToBytes(account);
byte[] output = new byte[32];
Sodium.crypto_generichash_blake2b_init(state, key, 0, 32);
Sodium.crypto_generichash_blake2b_update(state, source_b, source_b.length);
Sodium.crypto_generichash_blake2b_update(state, representative_b, representative_b.length);
Sodium.crypto_generichash_blake2b_update(state, account_b, account_b.length);
Sodium.crypto_generichash_blake2b_final(state, output, output.length);
return bytesToHex(output);
}
示例8
/**
* Compute hash to use to generate a receive work block
*
* @param previous Previous transation
* @param source Source address
* @return String of hash
*/
public static String computeReceiveHash(String previous, String source) {
Sodium sodium = NaCl.sodium();
byte[] state = new byte[Sodium.crypto_generichash_statebytes()];
byte[] key = new byte[Sodium.crypto_generichash_keybytes()];
byte[] previous_b = hexToBytes(previous);
byte[] source_b = hexToBytes(source);
byte[] output = new byte[32];
Sodium.crypto_generichash_blake2b_init(state, key, 0, 32);
Sodium.crypto_generichash_blake2b_update(state, previous_b, previous_b.length);
Sodium.crypto_generichash_blake2b_update(state, source_b, source_b.length);
Sodium.crypto_generichash_blake2b_final(state, output, output.length);
return bytesToHex(output);
}
示例9
/**
* Compute hash to use to generate a send work block
*
* @param previous Previous transation
* @param destination Destination address
* @param balance Raw NANO balance
* @return String of hash
*/
public static String computeSendHash(String previous, String destination, String balance) {
Sodium sodium = NaCl.sodium();
byte[] state = new byte[Sodium.crypto_generichash_statebytes()];
byte[] key = new byte[Sodium.crypto_generichash_keybytes()];
byte[] previous_b = hexToBytes(previous);
byte[] destination_b = hexToBytes(destination);
byte[] balance_b = hexToBytes(balance);
byte[] output = new byte[32];
Sodium.crypto_generichash_blake2b_init(state, key, 0, 32);
Sodium.crypto_generichash_blake2b_update(state, previous_b, previous_b.length);
Sodium.crypto_generichash_blake2b_update(state, destination_b, destination_b.length);
Sodium.crypto_generichash_blake2b_update(state, balance_b, balance_b.length);
Sodium.crypto_generichash_blake2b_final(state, output, output.length);
return bytesToHex(output);
}
示例10
/**
* Compute hash to use to generate a change work block
*
* @param previous Previous transaction
* @param representative Representative address
* @return String of hash
*/
public static String computeChangeHash(String previous, String representative) {
Sodium sodium = NaCl.sodium();
byte[] state = new byte[Sodium.crypto_generichash_statebytes()];
byte[] key = new byte[Sodium.crypto_generichash_keybytes()];
byte[] previous_b = hexToBytes(previous);
byte[] source_b = hexToBytes(representative);
byte[] output = new byte[32];
Sodium.crypto_generichash_blake2b_init(state, key, 0, 32);
Sodium.crypto_generichash_blake2b_update(state, previous_b, previous_b.length);
Sodium.crypto_generichash_blake2b_update(state, source_b, source_b.length);
Sodium.crypto_generichash_blake2b_final(state, output, output.length);
return bytesToHex(output);
}
示例11
/**
* Convert a Public Key to an Address
*
* @param public_key Public Key
* @return xrb address
*/
public static String publicToAddress(String public_key) {
Sodium sodium = NaCl.sodium();
byte[] bytePublic = NanoUtil.hexStringToByteArray(public_key);
String encodedAddress = encode(public_key);
byte[] state = new byte[Sodium.crypto_generichash_statebytes()];
byte[] key = new byte[Sodium.crypto_generichash_keybytes()];
byte[] check_b = new byte[5];
Sodium.crypto_generichash_blake2b_init(state, key, 0, 5);
Sodium.crypto_generichash_blake2b_update(state, bytePublic, bytePublic.length);
Sodium.crypto_generichash_blake2b_final(state, check_b, check_b.length);
reverse(check_b);
StringBuilder resultAddress = new StringBuilder();
resultAddress.insert(0, "nano_");
resultAddress.append(encodedAddress);
resultAddress.append(encode(NanoUtil.bytesToHex(check_b)));
return resultAddress.toString();
}
示例12
/**
* Convert an address to a public key
*
* @param encoded_address encoded Address
* @return Public Key
*/
public static String addressToPublic(String encoded_address) {
NaCl.sodium();
String data = encoded_address.split("_")[1].substring(0, 52);
byte[] data_b = NanoUtil.hexStringToByteArray(decodeAddressCharacters(data));
byte[] state = new byte[Sodium.crypto_generichash_statebytes()];
byte[] key = new byte[Sodium.crypto_generichash_keybytes()];
byte[] verify_b = new byte[5];
Sodium.crypto_generichash_blake2b_init(state, key, 0, 5);
Sodium.crypto_generichash_blake2b_update(state, data_b, data_b.length);
Sodium.crypto_generichash_blake2b_final(state, verify_b, verify_b.length);
reverse(verify_b);
// left pad byte array with zeros
StringBuilder pk = new StringBuilder(NanoUtil.bytesToHex(data_b));
while (pk.length() < 64) {
pk.insert(0, "0");
}
return pk.toString();
}
示例13
@Test
public void derivePublicFromPrivate() throws Exception {
// random key pair
Sodium sodium = NaCl.sodium();
byte[] pk = new byte[Sodium.crypto_sign_publickeybytes()];
byte[] sk = new byte[Sodium.crypto_sign_secretkeybytes()];
Sodium.crypto_sign_ed25519_keypair(pk, sk);
assertEquals(NanoUtil.bytesToHex(pk), NanoUtil.privateToPublic(NanoUtil.bytesToHex(sk)));
// random key pair from raiblocks official node
String priv = "49FF617E9074857402411B346D92174572EB5DE02CC9469C22E9681D8565E6D5";
String pub = "6C32F3E6ED921D2D98A3573B665FE7F8A35D510186AA9F1B365D283BBAA93DFB";
String account = "xrb_1u3kyhmgu6ix7pec8osueshyhy75doai53ocmwfmeqba9gxckhhurc3cokfo";
assertEquals(pub, NanoUtil.privateToPublic(priv));
}
示例14
@Test
public void testSealedBox() {
Sodium sodium= NaCl.sodium();
int ret=0;
long publickeylen=Sodium.crypto_box_publickeybytes();
long privatekeylen=Sodium.crypto_box_secretkeybytes();
final byte[] public_key=new byte[(int)publickeylen];
final byte[] private_key=new byte[(int)privatekeylen];
Sodium.crypto_box_keypair(public_key,private_key);
String message="testmessage";
byte[] ciphertext=new byte[Sodium.crypto_box_sealbytes()+message.length()];
Sodium.crypto_box_seal(ciphertext,message.getBytes(),message.length(),public_key);
byte[] plaintext=new byte[message.length()];
ret=Sodium.crypto_box_seal_open(plaintext,ciphertext,ciphertext.length,public_key,private_key);
Assert.assertEquals(0,ret);
}
示例15
@Test
public void testSealedBoxXChaCha20() {
Sodium sodium= NaCl.sodium();
int ret=0;
long publickeylen=Sodium.crypto_box_curve25519xchacha20poly1305_publickeybytes();
long privatekeylen=Sodium.crypto_box_curve25519xchacha20poly1305_secretkeybytes();
final byte[] public_key=new byte[(int)publickeylen];
final byte[] private_key=new byte[(int)privatekeylen];
Sodium.crypto_box_curve25519xchacha20poly1305_keypair(public_key,private_key);
String message="testmessage";
byte[] ciphertext=new byte[Sodium.crypto_box_curve25519xchacha20poly1305_sealbytes()+message.length()];
Sodium.crypto_box_curve25519xchacha20poly1305_seal(ciphertext,message.getBytes(),message.length(),public_key);
byte[] plaintext=new byte[message.length()];
ret=Sodium.crypto_box_curve25519xchacha20poly1305_seal_open(plaintext,ciphertext,ciphertext.length,public_key,private_key);
Assert.assertEquals(0,ret);
}
示例16
@Test
public void testGenericHashInit() {
Sodium sodium= NaCl.sodium();
String Password = "hunter2";
byte[] key = new byte[Sodium.crypto_box_seedbytes()];
byte[] passwd = Password.getBytes();
byte[] salt = new byte[]{ 88, (byte)240, (byte)185, 66, (byte)195, 101, (byte)160, (byte)138, (byte)137, 78, 1, 2, 3, 4, 5, 6};
Sodium.crypto_pwhash(
key,
key.length,
passwd,
passwd.length,
salt,
Sodium.crypto_pwhash_opslimit_interactive(),
Sodium.crypto_pwhash_memlimit_interactive(),
Sodium.crypto_pwhash_alg_default()
);
//assertEquals(key[0], (byte)49);
}
示例17
@Test
public void ChaCha20Poly1305() {
Sodium sodium= NaCl.sodium();
byte[] nonce = new byte[Sodium.crypto_aead_chacha20poly1305_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int mlen = message.length();
int[] clen_p = new int[0];
byte[] c = new byte[mlen + Sodium.crypto_aead_chacha20poly1305_abytes()];
int encryptReturn = Sodium.crypto_aead_chacha20poly1305_encrypt(c, clen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptReturn);
byte[] m = new byte[message.length()];
int[] mlen_p = new int[1];
Sodium.crypto_aead_chacha20poly1305_decrypt(m, mlen_p, new byte[0], c, c.length, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例18
@Test
public void ChaCha20Poly1305Ieft() {
Sodium sodium= NaCl.sodium();
byte[] nonce = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int mlen = message.length();
int[] clen_p = new int[0];
byte[] c = new byte[mlen + Sodium.crypto_aead_chacha20poly1305_ietf_abytes()];
int encryptReturn = Sodium.crypto_aead_chacha20poly1305_ietf_encrypt(c, clen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptReturn);
byte[] m = new byte[message.length()];
int[] mlen_p = new int[1];
Sodium.crypto_aead_chacha20poly1305_ietf_decrypt(m, mlen_p, new byte[0], c, c.length, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例19
@Test
public void XChaCha20Poly1305Ieft() {
Sodium sodium= NaCl.sodium();
byte[] nonce = new byte[Sodium.crypto_aead_xchacha20poly1305_ietf_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_xchacha20poly1305_ietf_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int mlen = message.length();
int[] clen_p = new int[0];
byte[] c = new byte[mlen + Sodium.crypto_aead_xchacha20poly1305_ietf_abytes()];
int encryptReturn = Sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(c, clen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptReturn);
byte[] m = new byte[message.length()];
int[] mlen_p = new int[1];
Sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(m, mlen_p, new byte[0], c, c.length, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例20
@Test
public void ChaCha20Poly1305Detached() {
Sodium sodium= NaCl.sodium();
int mlen = message.length();
byte[] c = new byte[mlen + Sodium.crypto_aead_chacha20poly1305_abytes()];
byte[] mac = new byte[Sodium.crypto_aead_chacha20poly1305_abytes()];
int[] maclen_p = new int[1];
byte[] nonce = new byte[Sodium.crypto_aead_chacha20poly1305_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int encryptDetachedReturn = Sodium.crypto_aead_chacha20poly1305_encrypt_detached(c, mac, maclen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptDetachedReturn);
byte[] m = new byte[message.length()];
Sodium.crypto_aead_chacha20poly1305_decrypt_detached(m, new byte[0], c, mlen, mac, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例21
@Test
public void ChaCha20Poly1305IeftDetached() {
Sodium sodium= NaCl.sodium();
int mlen = message.length();
byte[] c = new byte[mlen + Sodium.crypto_aead_chacha20poly1305_ietf_abytes()];
byte[] mac = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_abytes()];
int[] maclen_p = new int[1];
byte[] nonce = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int encryptDetachedReturn = Sodium.crypto_aead_chacha20poly1305_ietf_encrypt_detached(c, mac, maclen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptDetachedReturn);
byte[] m = new byte[message.length()];
Sodium.crypto_aead_chacha20poly1305_ietf_decrypt_detached(m, new byte[0], c, mlen, mac, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例22
@Test
public void XChaCha20Poly1305IeftDetached() {
Sodium sodium= NaCl.sodium();
int mlen = message.length();
byte[] c = new byte[mlen + Sodium.crypto_aead_xchacha20poly1305_ietf_abytes()];
byte[] mac = new byte[Sodium.crypto_aead_xchacha20poly1305_ietf_abytes()];
int[] maclen_p = new int[1];
byte[] nonce = new byte[Sodium.crypto_aead_xchacha20poly1305_ietf_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_xchacha20poly1305_ietf_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int encryptDetachedReturn = Sodium.crypto_aead_xchacha20poly1305_ietf_encrypt_detached(c, mac, maclen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptDetachedReturn);
byte[] m = new byte[message.length()];
Sodium.crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m, new byte[0], c, mlen, mac, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例23
@Test
public void testSealedBox() {
Sodium sodium= NaCl.sodium();
int ret=0;
long publickeylen=Sodium.crypto_box_publickeybytes();
long privatekeylen=Sodium.crypto_box_secretkeybytes();
final byte[] public_key=new byte[(int)publickeylen];
final byte[] private_key=new byte[(int)privatekeylen];
Sodium.crypto_box_keypair(public_key,private_key);
String message="testmessage";
byte[] ciphertext=new byte[Sodium.crypto_box_sealbytes()+message.length()];
Sodium.crypto_box_seal(ciphertext,message.getBytes(),message.length(),public_key);
byte[] plaintext=new byte[message.length()];
ret=Sodium.crypto_box_seal_open(plaintext,ciphertext,ciphertext.length,public_key,private_key);
Assert.assertEquals(0,ret);
}
示例24
@Test
public void testSealedBoxXChaCha20() {
Sodium sodium= NaCl.sodium();
int ret=0;
long publickeylen=Sodium.crypto_box_curve25519xchacha20poly1305_publickeybytes();
long privatekeylen=Sodium.crypto_box_curve25519xchacha20poly1305_secretkeybytes();
final byte[] public_key=new byte[(int)publickeylen];
final byte[] private_key=new byte[(int)privatekeylen];
Sodium.crypto_box_curve25519xchacha20poly1305_keypair(public_key,private_key);
String message="testmessage";
byte[] ciphertext=new byte[Sodium.crypto_box_curve25519xchacha20poly1305_sealbytes()+message.length()];
Sodium.crypto_box_curve25519xchacha20poly1305_seal(ciphertext,message.getBytes(),message.length(),public_key);
byte[] plaintext=new byte[message.length()];
ret=Sodium.crypto_box_curve25519xchacha20poly1305_seal_open(plaintext,ciphertext,ciphertext.length,public_key,private_key);
Assert.assertEquals(0,ret);
}
示例25
@Test
public void testGenericHashInit() {
Sodium sodium= NaCl.sodium();
String Password = "hunter2";
byte[] key = new byte[Sodium.crypto_box_seedbytes()];
byte[] passwd = Password.getBytes();
byte[] salt = new byte[]{ 88, (byte)240, (byte)185, 66, (byte)195, 101, (byte)160, (byte)138, (byte)137, 78, 1, 2, 3, 4, 5, 6};
Sodium.crypto_pwhash(
key,
key.length,
passwd,
passwd.length,
salt,
Sodium.crypto_pwhash_opslimit_interactive(),
Sodium.crypto_pwhash_memlimit_interactive(),
Sodium.crypto_pwhash_alg_default()
);
//assertEquals(key[0], (byte)49);
}
示例26
@Test
public void ChaCha20Poly1305() {
Sodium sodium= NaCl.sodium();
byte[] nonce = new byte[Sodium.crypto_aead_chacha20poly1305_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int mlen = message.length();
int[] clen_p = new int[0];
byte[] c = new byte[mlen + Sodium.crypto_aead_chacha20poly1305_abytes()];
int encryptReturn = Sodium.crypto_aead_chacha20poly1305_encrypt(c, clen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptReturn);
byte[] m = new byte[message.length()];
int[] mlen_p = new int[1];
Sodium.crypto_aead_chacha20poly1305_decrypt(m, mlen_p, new byte[0], c, c.length, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例27
@Test
public void ChaCha20Poly1305Ieft() {
Sodium sodium= NaCl.sodium();
byte[] nonce = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int mlen = message.length();
int[] clen_p = new int[0];
byte[] c = new byte[mlen + Sodium.crypto_aead_chacha20poly1305_ietf_abytes()];
int encryptReturn = Sodium.crypto_aead_chacha20poly1305_ietf_encrypt(c, clen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptReturn);
byte[] m = new byte[message.length()];
int[] mlen_p = new int[1];
Sodium.crypto_aead_chacha20poly1305_ietf_decrypt(m, mlen_p, new byte[0], c, c.length, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例28
@Test
public void XChaCha20Poly1305Ieft() {
Sodium sodium= NaCl.sodium();
byte[] nonce = new byte[Sodium.crypto_aead_xchacha20poly1305_ietf_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_xchacha20poly1305_ietf_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int mlen = message.length();
int[] clen_p = new int[0];
byte[] c = new byte[mlen + Sodium.crypto_aead_xchacha20poly1305_ietf_abytes()];
int encryptReturn = Sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(c, clen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptReturn);
byte[] m = new byte[message.length()];
int[] mlen_p = new int[1];
Sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(m, mlen_p, new byte[0], c, c.length, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例29
@Test
public void ChaCha20Poly1305Detached() {
Sodium sodium= NaCl.sodium();
int mlen = message.length();
byte[] c = new byte[mlen + Sodium.crypto_aead_chacha20poly1305_abytes()];
byte[] mac = new byte[Sodium.crypto_aead_chacha20poly1305_abytes()];
int[] maclen_p = new int[1];
byte[] nonce = new byte[Sodium.crypto_aead_chacha20poly1305_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int encryptDetachedReturn = Sodium.crypto_aead_chacha20poly1305_encrypt_detached(c, mac, maclen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptDetachedReturn);
byte[] m = new byte[message.length()];
Sodium.crypto_aead_chacha20poly1305_decrypt_detached(m, new byte[0], c, mlen, mac, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}
示例30
@Test
public void ChaCha20Poly1305IeftDetached() {
Sodium sodium= NaCl.sodium();
int mlen = message.length();
byte[] c = new byte[mlen + Sodium.crypto_aead_chacha20poly1305_ietf_abytes()];
byte[] mac = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_abytes()];
int[] maclen_p = new int[1];
byte[] nonce = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_npubbytes()];
byte[] key = new byte[Sodium.crypto_aead_chacha20poly1305_ietf_keybytes()];
Sodium.randombytes_buf(nonce, nonce.length);
Sodium.randombytes_buf(key, key.length);
int encryptDetachedReturn = Sodium.crypto_aead_chacha20poly1305_ietf_encrypt_detached(c, mac, maclen_p, message.getBytes(), mlen, new byte[0], 0, new byte[0], nonce, key);
assertEquals(0, encryptDetachedReturn);
byte[] m = new byte[message.length()];
Sodium.crypto_aead_chacha20poly1305_ietf_decrypt_detached(m, new byte[0], c, mlen, mac, new byte[0], 0, nonce, key);
assertEquals(message, new String(m));
}