How to use the secretbox function from tweetnacl

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

519
520
521
522
523
524
525
526
527
528
var salt = bcrypt.encodeBase64(Buffer.from(header.salt, 'hex'), 16);
var key = bcrypt.hashSync(password, "".concat(types.keystoreSaltPerfix).concat(salt));
key = _utils.Utils.sha256(Buffer.from(key).toString('hex'));
var keystoreData = Buffer.from(keystore_content.data, 'base64');
var nonce = keystoreData.slice(0, types.xchacha20NonceLength);
var privKey_full = nacl.secretbox.open(keystoreData.slice(types.xchacha20NonceLength), nonce, Buffer.from(key, 'hex'));

if (!privKey_full) {
  throw new _errors.SdkError('KeyStore parsing failed', _errors.CODES.Internal);
}
fork icon16
star icon8
watch icon0

1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
let possibleKey = groups[i].key

i += 1

try {
    decryptBox = nacl.secretbox.open(
        hexToUint(tx.sb),
        nonceFromTimestamp(tx.t),
        hexToUint(possibleKey)
    )
fork icon11
star icon19
watch icon5

90
91
92
93
94
95
96
97
98
99

var messageUInt8Array = nacl.util.decodeUTF8(msg);
var symEncryptionKey = nacl.randomBytes(nacl.secretbox.keyLength);
var symNonce = nacl.randomBytes(nacl.secretbox.nonceLength);

var symEncMessage = nacl.secretbox(messageUInt8Array, symNonce, symEncryptionKey);

if (theirPubKeyArray.length < 1) {
  throw new Error('Found no pubkeys to encrypt to.');
}
fork icon0
star icon0
watch icon2

+ 5 other calls in file

17
18
19
20
21
22
23
24
25
26
const nonceUTF8 = "aaaaaaaaaaaaaaaaaaaaaaaa";
const nonceBytes = utf8.encode(nonceUTF8);
const nonceBase64 = base64.encode(nonceBytes);
const testEncrypt = function(payload) {
  let payloadBytes = utf8.encode(JSON.stringify(payload));
  let bytes = nacl.secretbox(payloadBytes, nonceBytes, secretBytes);
  return base64.encode(bytes);
};

beforeEach(function() {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

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


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

+ 7 other calls in file

63
64
65
66
67
68
69
70
71
72

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

// encrypt
const encryptedMessage = nacl.secretbox(data, nonce, symmetricKey);

const noncePublicEncrypted = new Uint8Array(nonce.length + encryptedMessage.length);
noncePublicEncrypted.set(nonce, 0);
noncePublicEncrypted.set(encryptedMessage, nonce.length);
fork icon0
star icon0
watch icon0