Skip to content

Commit

Permalink
Merge pull request #654 from webmaster128/rng-cleanups
Browse files Browse the repository at this point in the history
Syntax and documentation improvements in passphrase utility - closes #663
  • Loading branch information
reyraa authored Apr 4, 2018
2 parents 652a9f8 + bceb0d6 commit 8fa5757
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/utils/passphrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const init = (rand = Math.random()) => {

/**
* - From a zero byte:
* - Removes all the 1s and replaces all the 1s with their index
* - Creates a random number with the length of resulting array (pos)
* - Removes all the 1s and replaces all the 0s with their index
* - Creates a random index (pos) of the resulting array
* - sets the bit in the pos position
* - creates random byte using crypto and assigns that to seed in the
* position of pos
Expand All @@ -59,10 +59,10 @@ const init = (rand = Math.random()) => {
* @returns {number[]} The input array whose member is pos is set
*/
export const generateSeed = ({ byte, seed, percentage, step } = init(), rand = Math.random()) => {
const available = byte.map((bit, index) => (!bit ? index : null)).filter(bit => (bit !== null));
const available = byte.map((bit, idx) => (!bit ? idx : null)).filter(idx => (idx !== null));
const seedIndex = (available.length > 0) ?
available[parseInt(rand * available.length, 10)] :
parseInt(rand * byte.length, 10);
available[Math.floor(rand * available.length)] :
Math.floor(rand * byte.length);

const content = leftPadd(crypto.randomBytes(1)[0].toString(16), '0', 2);

Expand Down
3 changes: 2 additions & 1 deletion src/utils/passphrase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ describe('Passphrase', () => {

it('should generate an array of 16 hex numbers as seed', () => {
const { seed } = generateSeed();
expect(seed.length).to.be.equal(16);
seed.forEach((num) => {
expect(parseInt(`0x${num}`, 10)).to.be.below(256);
expect(parseInt(num, 16)).to.be.below(256);
});
});
});
Expand Down

0 comments on commit 8fa5757

Please sign in to comment.