这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 13 additions & 47 deletions nacl-fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ var gf = function(init) {
return r;
};

function cleanup(arr) {
for (var i = 0; i < arr.length; i++) arr[i] = 0;
}

// Pluggable, initialized in high-level API below.
var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };
var randombytes = function(x, n , randomBytes) {
var i, v = randomBytes;
for (i = 0; i < n; i++) x[i] = v[i];
cleanup(v);
};

var _0 = new Uint8Array(16);
var _9 = new Uint8Array(32); _9[0] = 9;
Expand Down Expand Up @@ -1378,8 +1386,8 @@ function crypto_scalarmult_base(q, n) {
return crypto_scalarmult(q, n, _9);
}

function crypto_box_keypair(y, x) {
randombytes(x, 32);
function crypto_box_keypair(y, x, randomBytes) {
randombytes(x, 32, randomBytes);
return crypto_scalarmult_base(y, x);
}

Expand Down Expand Up @@ -2166,16 +2174,6 @@ function checkArrayTypes() {
}
}

function cleanup(arr) {
for (var i = 0; i < arr.length; i++) arr[i] = 0;
}

nacl.randomBytes = function(n) {
var b = new Uint8Array(n);
randombytes(b, n);
return b;
};

nacl.secretbox = function(msg, nonce, key) {
checkArrayTypes(msg, nonce, key);
checkLengths(key, nonce);
Expand Down Expand Up @@ -2243,10 +2241,10 @@ nacl.box.open = function(msg, nonce, publicKey, secretKey) {

nacl.box.open.after = nacl.secretbox.open;

nacl.box.keyPair = function() {
nacl.box.keyPair = function(randomBytes) {
var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
crypto_box_keypair(pk, sk);
crypto_box_keypair(pk, sk, randomBytes);
return {publicKey: pk, secretKey: sk};
};

Expand Down Expand Up @@ -2356,36 +2354,4 @@ nacl.verify = function(x, y) {
return (vn(x, 0, y, 0, x.length) === 0) ? true : false;
};

nacl.setPRNG = function(fn) {
randombytes = fn;
};

(function() {
// Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw.
var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;
if (crypto && crypto.getRandomValues) {
// Browsers.
var QUOTA = 65536;
nacl.setPRNG(function(x, n) {
var i, v = new Uint8Array(n);
for (i = 0; i < n; i += QUOTA) {
crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
}
for (i = 0; i < n; i++) x[i] = v[i];
cleanup(v);
});
} else if (typeof require !== 'undefined') {
// Node.js.
crypto = require('crypto');
if (crypto && crypto.randomBytes) {
nacl.setPRNG(function(x, n) {
var i, v = crypto.randomBytes(n);
for (i = 0; i < n; i++) x[i] = v[i];
cleanup(v);
});
}
}
})();

})(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));
60 changes: 13 additions & 47 deletions nacl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ var gf = function(init) {
return r;
};

function cleanup(arr) {
for (var i = 0; i < arr.length; i++) arr[i] = 0;
}

// Pluggable, initialized in high-level API below.
var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };
var randombytes = function(x, n , randomBytes) {
var i, v = randomBytes;
for (i = 0; i < n; i++) x[i] = v[i];
cleanup(v);
};

var _0 = new Uint8Array(16);
var _9 = new Uint8Array(32); _9[0] = 9;
Expand Down Expand Up @@ -440,8 +448,8 @@ function crypto_scalarmult_base(q, n) {
return crypto_scalarmult(q, n, _9);
}

function crypto_box_keypair(y, x) {
randombytes(x, 32);
function crypto_box_keypair(y, x, randomBytes) {
randombytes(x, 32, randomBytes);
return crypto_scalarmult_base(y, x);
}

Expand Down Expand Up @@ -953,16 +961,6 @@ function checkArrayTypes() {
}
}

function cleanup(arr) {
for (var i = 0; i < arr.length; i++) arr[i] = 0;
}

nacl.randomBytes = function(n) {
var b = new Uint8Array(n);
randombytes(b, n);
return b;
};

nacl.secretbox = function(msg, nonce, key) {
checkArrayTypes(msg, nonce, key);
checkLengths(key, nonce);
Expand Down Expand Up @@ -1030,10 +1028,10 @@ nacl.box.open = function(msg, nonce, publicKey, secretKey) {

nacl.box.open.after = nacl.secretbox.open;

nacl.box.keyPair = function() {
nacl.box.keyPair = function(randomBytes) {
var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
crypto_box_keypair(pk, sk);
crypto_box_keypair(pk, sk, randomBytes);
return {publicKey: pk, secretKey: sk};
};

Expand Down Expand Up @@ -1143,36 +1141,4 @@ nacl.verify = function(x, y) {
return (vn(x, 0, y, 0, x.length) === 0) ? true : false;
};

nacl.setPRNG = function(fn) {
randombytes = fn;
};

(function() {
// Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw.
var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;
if (crypto && crypto.getRandomValues) {
// Browsers.
var QUOTA = 65536;
nacl.setPRNG(function(x, n) {
var i, v = new Uint8Array(n);
for (i = 0; i < n; i += QUOTA) {
crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
}
for (i = 0; i < n; i++) x[i] = v[i];
cleanup(v);
});
} else if (typeof require !== 'undefined') {
// Node.js.
crypto = require('crypto');
if (crypto && crypto.randomBytes) {
nacl.setPRNG(function(x, n) {
var i, v = crypto.randomBytes(n);
for (i = 0; i < n; i++) x[i] = v[i];
cleanup(v);
});
}
}
})();

})(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));