pax_global_header00006660000000000000000000000064126052560100014507gustar00rootroot0000000000000052 comment=b7519fb8676104a1dc507099d407a40ca5ed4a6e
pseudorandombytes-2.0.0/000077500000000000000000000000001260525601000152555ustar00rootroot00000000000000pseudorandombytes-2.0.0/browser.js000066400000000000000000000005501260525601000172760ustar00rootroot00000000000000'use strict';
var PRNG = require('./prng');
var randombytes = require('randombytes');
try {
randombytes(8);
module.exports = randombytes;
} catch (e) {
module.exports = psudoRandomBytes;
}
var prng = new PRNG(function () {
return (Math.random().toString() + Date.now().toString());
});
function psudoRandomBytes(len) {
return prng.getBytes(len);
}
pseudorandombytes-2.0.0/chacha.js000066400000000000000000000043161260525601000170260ustar00rootroot00000000000000function ROTATE(v, c) {
return (v << c) | (v >>> (32 - c));
}
var createHash = require('create-hash');
function createU32(buf) {
var u8 = new Uint8Array(buf);
return new Uint32Array(u8.buffer);
}
function createState(buf) {
return createU32(createHash('sha512').update(buf).digest());
}
module.exports = Chacha20;
function Chacha20(key) {
this.input = createState(key);
this.cachePos = 64;
this.buffer = new Uint32Array(16);
this.output = new Buffer(64);
}
function quarterRound(x, a, b, c, d) {
x[a] += x[b]; x[d] = ROTATE(x[d] ^ x[a], 16);
x[c] += x[d]; x[b] = ROTATE(x[b] ^ x[c], 12);
x[a] += x[b]; x[d] = ROTATE(x[d] ^ x[a], 8);
x[c] += x[d]; x[b] = ROTATE(x[b] ^ x[c], 7);
}
Chacha20.prototype.makeBlock = function (output, start) {
var i = -1;
// copy input into working buffer
while (++i < 16) {
this.buffer[i] = this.input[i];
}
i = -1;
while (++i < 10) {
// straight round
quarterRound(this.buffer, 0, 4, 8,12);
quarterRound(this.buffer, 1, 5, 9,13);
quarterRound(this.buffer, 2, 6, 10,14);
quarterRound(this.buffer, 3, 7, 11,15);
//diaganle round
quarterRound(this.buffer, 0, 5,10,15);
quarterRound(this.buffer, 1, 6,11,12);
quarterRound(this.buffer, 2, 7, 8,13);
quarterRound(this.buffer, 3, 4, 9,14);
}
i = -1;
// copy working buffer into output
while (++i < 16) {
this.buffer[i] += this.input[i];
output.writeUInt32LE(this.buffer[i], start);
start += 4;
}
this.input[12]++;
};
Chacha20.prototype.getBytes = function(len) {
var dpos = 0;
var dst = new Buffer(len);
var cacheLen = 64 - this.cachePos;
if (cacheLen) {
if (cacheLen >= len) {
this.output.copy(dst, 0, this.cachePos, 64);
this.cachePos += len;
return dst;
} else {
this.output.copy(dst, 0, this.cachePos, 64);
len -= cacheLen;
dpos += cacheLen;
this.cachePos = 64;
}
}
while (len > 0 ) {
if (len <= 64) {
this.makeBlock(this.output, 0);
this.output.copy(dst, dpos, 0, len);
if (len < 64) {
this.cachePos = len;
}
return dst;
} else {
this.makeBlock(dst, dpos);
}
len -= 64;
dpos += 64;
}
throw new Error('something bad happended');
};
pseudorandombytes-2.0.0/index.js000066400000000000000000000000651260525601000167230ustar00rootroot00000000000000module.exports = require('crypto').pseudoRandomBytes;pseudorandombytes-2.0.0/package.json000066400000000000000000000007431260525601000175470ustar00rootroot00000000000000{
"name": "pseudorandombytes",
"version": "2.0.0",
"description": "",
"main": "index.js",
"browser": "browser.js",
"scripts": {
"test": "node test.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"create-hash": "^1.1.2",
"create-hmac": "^1.1.4",
"randombytes": "^2.0.0"
},
"devDependencies": {
"through2": "^0.6.3"
},
"repository": {
"type": "git",
"url": "git@github.com:crypto-browserify/pseudorandombytes.git"
}
}
pseudorandombytes-2.0.0/prng.js000066400000000000000000000010661260525601000165640ustar00rootroot00000000000000var ChaCha = require('./chacha');
var createHmac = require('create-hmac')
module.exports = PRNG;
function PRNG(seed, _maxInt) {
this._seed = seed;
this.chacha = new ChaCha(this._seed());
}
PRNG.prototype.seed = function () {
var blendedSeed = createHmac('sha512', this.chacha.getBytes(64))
.update(this._seed())
.digest();
this.chacha = new ChaCha(blendedSeed);
};
PRNG.prototype._seed = function () {
throw new Error('you must impliment me');
};
PRNG.prototype.getBytes = function (len) {
this.seed(len);
return this.chacha.getBytes(len);
};
pseudorandombytes-2.0.0/readme.md000066400000000000000000000011671260525601000170410ustar00rootroot00000000000000pseudoRandomBytes
===
crypto.pseudoRandomBytes but for use with browserify. In node it just returns crypto.pseudoRandomBytes but in the browser it uses [crypto-browserify/randombytes](https://github.com/crypto-browserify/randombytes) if available else it seeds a prng based on [chacha20poly1305](https://github.com/calvinmetcalf/chacha20poly1305).
To use:
====
```js
var pseudoRandomBytes = require('pseudorandomBbytes');
var bytes = pseudoRandomBytes(34);
```
To be clear there is no legitimate reason for you ever to want to use this.
# The algorithms used in this module are **NOT** suitable for cryptographical usage.
pseudorandombytes-2.0.0/test.js000066400000000000000000000007211260525601000165720ustar00rootroot00000000000000'use strict';
var spawn = require('child_process').spawn;
var child = spawn('ent', ['-b']);
var prng = require('./browser');
var Readable = require('stream').Readable;
var bytes = 1024 * 1024 * 1024;
var stream = new Readable();
var sofar = 0;
stream._read = function (num) {
this.push(prng(num));
sofar += num;
if (sofar > bytes) {
this.push(null);
}
};
stream.pipe(child.stdin);
child.stdout.on('data',function (d) {
console.log(d.toString());
});