-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
71 lines (58 loc) · 2.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const sodium = require('sodium-native')
const uniform = require('./unbiased-uniform')
const double = require('./unbiased-double')
const shuffle = require('./unbiased-shuffle')
const sample = require('./unbiased-sample')
const weighted = require('./unbiased-weighted-sample')
const weightedInteger = require('./unbiased-weighted-sample-integer')
module.exports = function (key, nonce) {
var state = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
sodium.crypto_stream_chacha20_xor_init(state, nonce, key)
return create(sodium.crypto_stream_chacha20_xor_update, state, key, nonce)
}
module.exports.xchacha = function (key, nonce) {
var state = Buffer.alloc(sodium.crypto_stream_xchacha20_xor_STATEBYTES)
sodium.crypto_stream_xchacha20_xor_init(state, nonce, key)
return create(sodium.crypto_stream_xchacha20_xor_update, state, key, nonce)
}
module.exports.salsa = function (key, nonce) {
var state = Buffer.alloc(sodium.crypto_stream_salsa20_xor_STATEBYTES)
sodium.crypto_stream_salsa20_xor_init(state, nonce, key)
return create(sodium.crypto_stream_salsa20_xor_update, state, key, nonce)
}
module.exports.xsalsa = function (key, nonce) {
var state = Buffer.alloc(sodium.crypto_stream_xsalsa20_xor_STATEBYTES)
sodium.crypto_stream_xsalsa20_xor_init(state, nonce, key)
return create(sodium.crypto_stream_xsalsa20_xor_update, state, key, nonce)
}
function create (method, state, key, nonce) {
function next (bytes, buf) {
if (buf == null) buf = Buffer.alloc(bytes)
else {
buf = buf.subarray(0, bytes)
buf.fill(0)
}
// Can be optimised if we export the proper function from sodium
method(state, buf, buf)
next.bytes += bytes
next.trails++
return buf
}
next.bytes = 0
next.trails = 0
next.uniform = uniform.bind(null, next)
next.double = double.bind(null, next)
next.shuffle = shuffle.bind(null, next)
next.sample = sample.bind(null, next)
next.weighted = weighted.bind(null, next)
next.weightedInteger = weightedInteger.bind(null, next)
return next
}
module.exports.NONCEBYTES = sodium.crypto_stream_chacha20_NONCEBYTES
module.exports.KEYBYTES = sodium.crypto_stream_chacha20_KEYBYTES
module.exports.xchacha.NONCEBYTES = sodium.crypto_stream_xchacha20_NONCEBYTES
module.exports.xchacha.KEYBYTES = sodium.crypto_stream_xchacha20_KEYBYTES
module.exports.salsa.NONCEBYTES = sodium.crypto_stream_salsa20_NONCEBYTES
module.exports.salsa.KEYBYTES = sodium.crypto_stream_salsa20_KEYBYTES
module.exports.xsalsa.NONCEBYTES = sodium.crypto_stream_xsalsa20_NONCEBYTES
module.exports.xsalsa.KEYBYTES = sodium.crypto_stream_xsalsa20_KEYBYTES