UNPKG

864 BJavaScriptView Raw
1/* eslint-disable camelcase */
2const xsalsa20 = require('xsalsa20')
3
4if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
5
6exports.crypto_stream_KEYBYTES = 32
7exports.crypto_stream_NONCEBYTES = 24
8exports.crypto_stream_PRIMITIVE = 'xsalsa20'
9
10exports.crypto_stream = function (c, nonce, key) {
11 c.fill(0)
12 exports.crypto_stream_xor(c, c, nonce, key)
13}
14
15exports.crypto_stream_xor = function (c, m, nonce, key) {
16 const xor = xsalsa20(nonce, key)
17
18 xor.update(m, c)
19 xor.final()
20}
21
22exports.crypto_stream_xor_instance = function (nonce, key) {
23 return new XOR(nonce, key)
24}
25
26function XOR (nonce, key) {
27 this._instance = xsalsa20(nonce, key)
28}
29
30XOR.prototype.update = function (out, inp) {
31 this._instance.update(inp, out)
32}
33
34XOR.prototype.final = function () {
35 this._instance.finalize()
36 this._instance = null
37}