2020-06-17 11:06:30 +00:00
|
|
|
const assert = require('nanoassert')
|
|
|
|
|
2020-05-04 19:08:29 +00:00
|
|
|
module.exports = {
|
|
|
|
crypto_verify_16,
|
2020-06-17 11:07:28 +00:00
|
|
|
crypto_verify_32,
|
2020-06-17 11:19:05 +00:00
|
|
|
sodium_memcmp,
|
|
|
|
sodium_is_zero
|
2020-05-04 19:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 22:56:59 +00:00
|
|
|
function vn (x, xi, y, yi, n) {
|
|
|
|
var i, d = 0
|
|
|
|
for (i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i]
|
|
|
|
return (1 & ((d - 1) >>> 8)) - 1
|
2020-05-04 19:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function crypto_verify_16(x, xi, y, yi) {
|
2020-06-16 22:56:59 +00:00
|
|
|
return vn(x, xi, y, yi, 16)
|
2020-05-04 19:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function crypto_verify_32(x, xi, y, yi) {
|
2020-06-16 22:56:59 +00:00
|
|
|
return vn(x, xi, y, yi, 32)
|
2020-05-04 19:08:29 +00:00
|
|
|
}
|
2020-06-17 11:06:30 +00:00
|
|
|
|
|
|
|
function sodium_memcmp (a, b) {
|
2020-06-17 12:44:35 +00:00
|
|
|
assert(a.byteLength === b.byteLength, 'buffers must be the same size')
|
2020-06-17 11:06:30 +00:00
|
|
|
|
2020-06-17 11:13:05 +00:00
|
|
|
return vn(a, 0, b, 0, a.byteLength) === 0
|
2020-06-17 11:06:30 +00:00
|
|
|
}
|
2020-06-17 11:19:05 +00:00
|
|
|
|
2020-06-17 11:31:54 +00:00
|
|
|
function sodium_is_zero (arr) {
|
2020-06-17 11:19:05 +00:00
|
|
|
var d = 0
|
|
|
|
for (let i = 0; i < arr.length; i++) d |= arr[i]
|
|
|
|
return d === 0
|
|
|
|
}
|