}\r
\r
export const 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
+ 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
export const 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 tmp = new Uint8Array(signedMsg.length);\r
- var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);\r
- if (mlen < 0) return null;\r
- var m = new Uint8Array(mlen);\r
- for (var i = 0; i < m.length; i++) m[i] = tmp[i];\r
- return m;\r
+ checkArrayTypes(signedMsg, publicKey);\r
+ if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
+ throw new Error('bad public key size');\r
+ var tmp = new Uint8Array(signedMsg.length);\r
+ var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);\r
+ if (mlen < 0) return null;\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
export const detached = function(msg, secretKey) {\r
- var signedMsg = 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
+ var signedMsg = 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
export const 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
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
- throw new Error('bad public key size');\r
- var sm = new Uint8Array(crypto_sign_BYTES + msg.length);\r
- var m = new Uint8Array(crypto_sign_BYTES + msg.length);\r
- var i;\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
+ checkArrayTypes(msg, sig, publicKey);\r
+ if (sig.length !== crypto_sign_BYTES)\r
+ throw new Error('bad signature size');\r
+ if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
+ throw new Error('bad public key size');\r
+ var sm = new Uint8Array(crypto_sign_BYTES + msg.length);\r
+ var m = new Uint8Array(crypto_sign_BYTES + msg.length);\r
+ var i;\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
-export const keyPair = function() {\r
+export const keyPair = {\r
+ create() {\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
-keyPair.fromSecretKey = function(secretKey) {\r
+ fromSecretKey(secretKey) {\r
checkArrayTypes(secretKey);\r
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\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
-keyPair.fromSeed = function(seed) {\r
+ fromSeed(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 const publicKeyLength = crypto_sign_PUBLICKEYBYTES;\r
export const secretKeyLength = crypto_sign_SECRETKEYBYTES;\r
}\r
}\r
})();\r
+\r
+export const nacl = { sign, open, detached, verify, keyPair, publicKeyLength, secretKeyLength, seedLength, signatureLength }
\ No newline at end of file