\r
/* High-level API */\r
\r
- function checkLengths(k, n) {\r
- if (k.length !== crypto_secretbox_KEYBYTES) throw new Error('bad key size');\r
- if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error('bad nonce size');\r
- }\r
-\r
function checkArrayTypes() {\r
for (var i = 0; i < arguments.length; i++) {\r
if (!(arguments[i] instanceof Uint8Array))\r
for (var i = 0; i < arr.length; i++) arr[i] = 0;\r
}\r
\r
- secretbox = function(msg, nonce, key) {\r
- checkArrayTypes(msg, nonce, key);\r
- checkLengths(key, nonce);\r
- var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);\r
- var c = new Uint8Array(m.length);\r
- for (var i = 0; i < msg.length; i++) m[i+crypto_secretbox_ZEROBYTES] = msg[i];\r
- crypto_secretbox(c, m, m.length, nonce, key);\r
- return c.subarray(crypto_secretbox_BOXZEROBYTES);\r
- };\r
-\r
- secretbox.open = function(box, nonce, key) {\r
- checkArrayTypes(box, nonce, key);\r
- checkLengths(key, nonce);\r
- var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);\r
- var m = new Uint8Array(c.length);\r
- for (var i = 0; i < box.length; i++) c[i+crypto_secretbox_BOXZEROBYTES] = box[i];\r
- if (c.length < 32) return null;\r
- if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;\r
- return m.subarray(crypto_secretbox_ZEROBYTES);\r
- };\r
-\r
- secretbox.keyLength = crypto_secretbox_KEYBYTES;\r
- secretbox.nonceLength = crypto_secretbox_NONCEBYTES;\r
- secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;\r
-\r
const sign = function(msg, secretKey) {\r
checkArrayTypes(msg, secretKey);\r
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r