How to use the randomBytes function from tweetnacl

Find comprehensive JavaScript tweetnacl.randomBytes code examples handpicked from public code repositorys.

37
38
39
40
41
42
43
44
45
46
 * @returns {String} ciphertext - base64-encoded ciphertext
 * @returns {String} nonce - base64-encoded nonce
 */
var encryptAsymmetric = function (_a) {
    var plaintext = _a.plaintext, publicKey = _a.publicKey, privateKey = _a.privateKey;
    var nonce = nacl.randomBytes(24);
    var ciphertext = nacl.box(util.decodeUTF8(plaintext), nonce, util.decodeBase64(publicKey), util.decodeBase64(privateKey));
    return {
        ciphertext: util.encodeBase64(ciphertext),
        nonce: util.encodeBase64(nonce)
fork icon2
star icon7
watch icon2

+ 8 other calls in file

25
26
27
28
29
30
31
32
33
34
}

var privKey = keystore.exportPrivateKey(myPubKey, pwDerivedKey, hdPathString);
var privKeyUInt8Array = nacl_decodeHex(privKey);
var pubKeyUInt8Array = nacl_decodeHex(theirPubKey);
var nonce = nacl.randomBytes(nacl.box.nonceLength);
var encryptedMessage = nacl.box(msgUint8Array, nonce, pubKeyUInt8Array, privKeyUInt8Array);

var output = {
  alg: 'curve25519-xsalsa20-poly1305',
fork icon0
star icon0
watch icon2

+ 8 other calls in file

107
108
109
110
111
112
113
114
115
116
117
118
  }
};


KeyStore._encryptString = function (string, pwDerivedKey) {


  var nonce = nacl.randomBytes(nacl.secretbox.nonceLength);
  var encObj = nacl.secretbox(nacl.util.decodeUTF8(string), nonce, pwDerivedKey);
  var encString = { 'encStr': nacl.util.encodeBase64(encObj),
                    'nonce': nacl.util.encodeBase64(nonce)};
  return encString;
fork icon0
star icon0
watch icon0

+ 3 other calls in file

22
23
24
25
26
27
28
29
30
    throw new Error("Missing data parameter");
}

// generate ephemeral keypair
const ephemeralKeyPair = nacl.box.keyPair();
const nonce = nacl.randomBytes(nacl.box.nonceLength);

// encrypt
const encryptedMessage = nacl.box(data, nonce, publicKey, ephemeralKeyPair.secretKey);
fork icon0
star icon0
watch icon0

+ 2 other calls in file