1 |
|
2 | const xsalsa20 = require('xsalsa20')
|
3 |
|
4 | if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
|
5 |
|
6 | exports.crypto_stream_KEYBYTES = 32
|
7 | exports.crypto_stream_NONCEBYTES = 24
|
8 | exports.crypto_stream_PRIMITIVE = 'xsalsa20'
|
9 |
|
10 | exports.crypto_stream = function (c, nonce, key) {
|
11 | c.fill(0)
|
12 | exports.crypto_stream_xor(c, c, nonce, key)
|
13 | }
|
14 |
|
15 | exports.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 |
|
22 | exports.crypto_stream_xor_instance = function (nonce, key) {
|
23 | return new XOR(nonce, key)
|
24 | }
|
25 |
|
26 | function XOR (nonce, key) {
|
27 | this._instance = xsalsa20(nonce, key)
|
28 | }
|
29 |
|
30 | XOR.prototype.update = function (out, inp) {
|
31 | this._instance.update(inp, out)
|
32 | }
|
33 |
|
34 | XOR.prototype.final = function () {
|
35 | this._instance.finalize()
|
36 | this._instance = null
|
37 | }
|