2020-06-24 12:08:55 +00:00
|
|
|
/* eslint-disable camelcase */
|
|
|
|
const xsalsa20 = require('xsalsa20')
|
2017-06-28 09:11:38 +00:00
|
|
|
|
2020-06-18 15:09:03 +00:00
|
|
|
if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
|
2017-06-28 09:11:38 +00:00
|
|
|
|
2020-06-18 12:11:22 +00:00
|
|
|
if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
|
|
|
|
|
2017-06-28 09:11:38 +00:00
|
|
|
exports.crypto_stream_KEYBYTES = 32
|
|
|
|
exports.crypto_stream_NONCEBYTES = 24
|
|
|
|
exports.crypto_stream_PRIMITIVE = 'xsalsa20'
|
2020-09-22 12:47:36 +00:00
|
|
|
exports.crypto_stream_xsalsa20_MESSAGEBYTES_MAX = Number.MAX_SAFE_INTEGER
|
2017-06-28 09:11:38 +00:00
|
|
|
|
2020-06-12 15:06:31 +00:00
|
|
|
exports.crypto_stream = function (c, nonce, key) {
|
2020-05-04 16:47:52 +00:00
|
|
|
c.fill(0)
|
2020-06-12 15:06:31 +00:00
|
|
|
exports.crypto_stream_xor(c, c, nonce, key)
|
2017-06-28 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 15:06:31 +00:00
|
|
|
exports.crypto_stream_xor = function (c, m, nonce, key) {
|
2020-06-24 12:08:55 +00:00
|
|
|
const xor = xsalsa20(nonce, key)
|
2020-05-04 16:47:52 +00:00
|
|
|
|
|
|
|
xor.update(m, c)
|
2017-06-28 09:11:38 +00:00
|
|
|
xor.final()
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.crypto_stream_xor_instance = function (nonce, key) {
|
|
|
|
return new XOR(nonce, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
function XOR (nonce, key) {
|
|
|
|
this._instance = xsalsa20(nonce, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
XOR.prototype.update = function (out, inp) {
|
|
|
|
this._instance.update(inp, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
XOR.prototype.final = function () {
|
|
|
|
this._instance.finalize()
|
|
|
|
this._instance = null
|
|
|
|
}
|