· 9 years ago · Nov 21, 2016, 10:50 AM
1 public void exportKey(String password) {
2 createSecretBox();
3
4 byte[] seed = identityPrivateKey.toBytes();
5 String seedStr = encode64(seed);
6 String obj = "{\"Seed\":\"" + seedStr + "\"}";
7 byte[] salt = generateSalt();
8 byte[] nonce = generateNonce();
9 byte[] passwd = decode64(password);
10
11 byte[] secretKey = new byte[0];
12 sodium.crypto_pwhash_scryptsalsa208sha256(
13 secretKey,
14 sodium.crypto_secretbox_keybytes(),
15 passwd,
16 passwd.length,
17 salt,
18 sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(),
19 sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_interactive());
20
21//?
22// String secretKeyStr = hash.pwhash_scryptsalsa208sha256(
23// password,
24// encoder,
25// salt,
26// sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(),
27// sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_interactive());
28
29 byte[] encryptedObj = new byte[0];
30 sodium.crypto_secretbox_easy(
31 encryptedObj,
32 decode64(obj),
33 obj.length(),
34 nonce,
35 secretKey);
36
37 String fullEncryptedObjStr = "{\"Key\":\"" + encryptedObj + "\","
38 + " \"Salt\":\"" + encode64(salt) + "\","
39 + " \"Nonce\":\"" + encode64(nonce) + "\"}";
40
41 L.d("exportKey", fullEncryptedObjStr);
42 }