// See for details: https://docs.nano.org/integration-guides/the-basics/\r
// Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js\r
\r
-/**\r
-* Generate a public key from a private key using the Ed25519 algorithm. The key\r
-* should be a cryptographically strong random value.\r
-*\r
-* @param {string} privateKey - 32-byte private key\r
-* @returns {string} 32-byte public key\r
-*/\r
-function getPublicKey (privateKey: Uint8Array): Uint8Array {\r
- const h = blake2b(64).update(privateKey).digest().slice(0, 32)\r
- return scalarMult.base(h)\r
- // const pk = new Uint8Array(32)\r
- // const p = [gf(),gf(),gf(),gf()]\r
- // const h = blake2b(64).update(privateKey).digest().slice(0, 32)\r
-\r
- // h[0] &= 0xf8\r
- // h[31] &= 0x7f\r
- // h[31] |= 0x40\r
-\r
- // scalarbase(p, h)\r
- // pack(pk, p)\r
-\r
- // return pk\r
-}\r
var gf = function(init?) {\r
var i, r = new Float64Array(16);\r
if (init) for (i = 0; i < init.length; i++) r[i] = init[i];\r
};\r
\r
// Pluggable, initialized in high-level API below.\r
- var randombytes = function(x, n) { throw new Error('no PRNG'); };\r
+ var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };\r
\r
var _0 = new Uint8Array(16);\r
var _9 = new Uint8Array(32); _9[0] = 9;\r
crypto_sign_SEEDBYTES = 32,\r
crypto_hash_BYTES = 64;\r
\r
- export const lowlevel = {\r
+ nacl.lowlevel = {\r
crypto_core_hsalsa20: crypto_core_hsalsa20,\r
crypto_stream_xor: crypto_stream_xor,\r
crypto_stream: crypto_stream,\r
if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error('bad secret key size');\r
}\r
\r
- function checkArrayTypes(...args) {\r
- for (var i = 0; i < args.length; i++) {\r
- if (!(args[i] instanceof Uint8Array))\r
+ function checkArrayTypes() {\r
+ for (var i = 0; i < arguments.length; i++) {\r
+ if (!(arguments[i] instanceof Uint8Array))\r
throw new TypeError('unexpected type, use Uint8Array');\r
}\r
}\r
for (var i = 0; i < arr.length; i++) arr[i] = 0;\r
}\r
\r
- const randomBytes = function(n) {\r
+ nacl.randomBytes = function(n) {\r
var b = new Uint8Array(n);\r
randombytes(b, n);\r
return b;\r
};\r
\r
- const scalarMult = function(n, p) {\r
+ nacl.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
+ nacl.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
+ nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES;\r
+ nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;\r
+ nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;\r
+ \r
+ nacl.scalarMult = function(n, p) {\r
checkArrayTypes(n, p);\r
if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');\r
if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size');\r
return q;\r
};\r
\r
- scalarMult.base = function(n) {\r
+ nacl.scalarMult.base = function(n) {\r
checkArrayTypes(n);\r
if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');\r
var q = new Uint8Array(crypto_scalarmult_BYTES);\r
return q;\r
};\r
\r
- scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;\r
- scalarMult.groupElementLength = crypto_scalarmult_BYTES;\r
+ nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;\r
+ nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES;\r
\r
- const box = {\r
- before: function(publicKey, secretKey) {\r
+ nacl.box = function(msg, nonce, publicKey, secretKey) {\r
+ var k = nacl.box.before(publicKey, secretKey);\r
+ return nacl.secretbox(msg, nonce, k);\r
+ };\r
+ \r
+ nacl.box.before = function(publicKey, secretKey) {\r
checkArrayTypes(publicKey, secretKey);\r
checkBoxLengths(publicKey, secretKey);\r
var k = new Uint8Array(crypto_box_BEFORENMBYTES);\r
crypto_box_beforenm(k, publicKey, secretKey);\r
return k;\r
- },\r
- keyPair: {\r
- create: () => {\r
+ };\r
+ \r
+ nacl.box.after = nacl.secretbox;\r
+ \r
+ nacl.box.open = function(msg, nonce, publicKey, secretKey) {\r
+ var k = nacl.box.before(publicKey, secretKey);\r
+ return nacl.secretbox.open(msg, nonce, k);\r
+ };\r
+ \r
+ nacl.box.open.after = nacl.secretbox.open;\r
+ \r
+ nacl.box.keyPair = function() {\r
var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);\r
var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);\r
crypto_box_keypair(pk, sk);\r
return {publicKey: pk, secretKey: sk};\r
- },\r
- fromSecretKey: (secretKey) => {\r
+ };\r
+ \r
+ nacl.box.keyPair.fromSecretKey = function(secretKey) {\r
checkArrayTypes(secretKey);\r
if (secretKey.length !== crypto_box_SECRETKEYBYTES)\r
throw new Error('bad secret key size');\r
var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);\r
crypto_scalarmult_base(pk, secretKey);\r
return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
- }\r
- },\r
- publicKeyLength: crypto_box_PUBLICKEYBYTES,\r
- secretKeyLength: crypto_box_SECRETKEYBYTES,\r
- sharedKeyLength: crypto_box_BEFORENMBYTES,\r
- nonceLength: crypto_box_NONCEBYTES\r
- }\r
-\r
- const message = {\r
- sign: (msg, secretKey) => {\r
+ };\r
+ \r
+ nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;\r
+ nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES;\r
+ nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES;\r
+ nacl.box.nonceLength = crypto_box_NONCEBYTES;\r
+ nacl.box.overheadLength = nacl.secretbox.overheadLength;\r
+ \r
+ nacl.sign = function(msg, secretKey) {\r
checkArrayTypes(msg, secretKey);\r
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
throw new Error('bad secret key size');\r
var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length);\r
crypto_sign(signedMsg, msg, msg.length, secretKey);\r
return signedMsg;\r
- },\r
+ };\r
\r
- open: (signedMsg, publicKey) => {\r
+ nacl.sign.open = function(signedMsg, publicKey) {\r
checkArrayTypes(signedMsg, publicKey);\r
if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
throw new Error('bad public key size');\r
var m = new Uint8Array(mlen);\r
for (var i = 0; i < m.length; i++) m[i] = tmp[i];\r
return m;\r
- },\r
+ };\r
\r
- detach: (msg, secretKey) => {\r
+ nacl.sign.detached = function(msg, secretKey) {\r
var signedMsg = nacl.sign(msg, secretKey);\r
var sig = new Uint8Array(crypto_sign_BYTES);\r
for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];\r
return sig;\r
- },\r
+ };\r
\r
- verify:(msg, sig, publicKey) => {\r
+ nacl.sign.detached.verify = function(msg, sig, publicKey) {\r
checkArrayTypes(msg, sig, publicKey);\r
if (sig.length !== crypto_sign_BYTES)\r
throw new Error('bad signature size');\r
for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];\r
for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i];\r
return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);\r
- }\r
- }\r
+ };\r
\r
- const keyPair = {\r
- create: () => {\r
+ nacl.sign.keyPair = function() {\r
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);\r
crypto_sign_keypair(pk, sk);\r
return {publicKey: pk, secretKey: sk};\r
- },\r
+ };\r
\r
- fromSecretKey: (secretKey) => {\r
+ nacl.sign.keyPair.fromSecretKey = function(secretKey) {\r
checkArrayTypes(secretKey);\r
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
- throw new Error(`bad secret key size ${secretKey.length}`);\r
+ throw new Error('bad secret key size');\r
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];\r
return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
- },\r
+ };\r
\r
- fromSeed: (seed) => {\r
+ nacl.sign.keyPair.fromSeed = function(seed) {\r
checkArrayTypes(seed);\r
if (seed.length !== crypto_sign_SEEDBYTES)\r
throw new Error('bad seed size');\r
for (var i = 0; i < 32; i++) sk[i] = seed[i];\r
crypto_sign_keypair(pk, sk, true);\r
return {publicKey: pk, secretKey: sk};\r
- }\r
- }\r
+ };\r
\r
- export let publicKeyLength = crypto_sign_PUBLICKEYBYTES;\r
- export let secretKeyLength = crypto_sign_SECRETKEYBYTES;\r
- export let seedLength = crypto_sign_SEEDBYTES;\r
- export let signatureLength = crypto_sign_BYTES;\r
+ nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;\r
+ nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;\r
+ nacl.sign.seedLength = crypto_sign_SEEDBYTES;\r
+ nacl.sign.signatureLength = crypto_sign_BYTES;\r
\r
- const hash = function(msg) {\r
+ nacl.hash = function(msg) {\r
checkArrayTypes(msg);\r
var h = new Uint8Array(crypto_hash_BYTES);\r
crypto_hash(h, msg, msg.length);\r
return h;\r
};\r
\r
- hash.hashLength = crypto_hash_BYTES;\r
+ nacl.hash.hashLength = crypto_hash_BYTES;\r
\r
- const verify = function(x, y) {\r
+ nacl.verify = function(x, y) {\r
checkArrayTypes(x, y);\r
// Zero length arguments are considered not equal.\r
if (x.length === 0 || y.length === 0) return false;\r
return (vn(x, 0, y, 0, x.length) === 0) ? true : false;\r
};\r
\r
- const setPRNG = function(fn) {\r
+ nacl.setPRNG = function(fn) {\r
randombytes = fn;\r
};\r
\r