Skip to content

Commit

Permalink
Replace crypto.pseudoRandomBytes with something faster
Browse files Browse the repository at this point in the history
(and probably of lower quality, not that it really matters)
  • Loading branch information
animetosho committed May 8, 2016
1 parent facc390 commit 838a3dc
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions lib/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,44 @@ MultiEncoder.maxSize = function(size, line_size) {
*/
};

// something that isn't as slow as crypto.pseudoRandomBytes
var randString = function() {
return String.fromCharCode(
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26,
65 + Math.random()*26,
97 + Math.random()*26
);
};

var crypto = require('crypto');
function Post(headers, pool, date) {
this.headers = {};
this.postPos = 0;
this.pool = pool;
this.genTime = date ? date.getTime() : Date.now();

this.messageId = crypto.pseudoRandomBytes(16).toString('hex') + '-' + this.genTime + '@nyuu';
// TODO: consider writing Message-ID as the first header - this simplifies some logic
this.messageId = randString() + '-' + this.genTime + '@nyuu';
if(pool) {
this.buf = pool.get();
while(1) {
Expand Down Expand Up @@ -204,9 +233,9 @@ Post.prototype = {
bufs: null,

randomizeMessageID: function() {
var rnd = crypto.pseudoRandomBytes(16).toString('hex');
var rnd = randString();
this.data.write(rnd, this.mIdPos, ENCODING);
return this.messageId = rnd + this.messageId.substr(32);
return this.messageId = rnd + this.messageId.substr(24);
},

release: function() {
Expand Down

0 comments on commit 838a3dc

Please sign in to comment.