Skip to content

Commit

Permalink
Added new generateDHKey and generatePrime functions
Browse files Browse the repository at this point in the history
- Added my logger package.
- Moved JavaScript into new src/ dir.
  • Loading branch information
btoll committed Dec 15, 2016
1 parent e843339 commit ab3cf05
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
4 changes: 4 additions & 0 deletions bin/sneak
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ if ((index = argv.indexOf('-e')) > -1) {
console.log(sneak.encode(argv[index + 1], bignum(argv[index + 2])));
} else if ((index = argv.indexOf('-d')) > -1) {
console.log(sneak.decode(argv[index + 1], bignum(argv[index + 2])));
} else if ((index = argv.indexOf('generateDHKey')) > -1) {
console.log(sneak.generateDHKey());
} else if ((index = argv.indexOf('generateKey')) > -1) {
console.log(sneak.generateKey());
} else if ((index = argv.indexOf('generatePrime')) > -1) {
console.log(sneak.generatePrime());
} else {
console.log('Unrecognized command');
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bin": {
"sneak": "./bin/sneak"
},
"main": "index.js",
"main": "./src/index.js",
"keywords": [
"xor",
"base64",
Expand All @@ -30,7 +30,8 @@
"test": "npm run clean && npm run lint && npm run specs"
},
"dependencies": {
"bignum": "^0.12.5"
"bignum": "^0.12.5",
"logger": "git+https://github.com/btoll/logger.git"
}
}

2 changes: 1 addition & 1 deletion spec/sneak-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const bignum = require('bignum');
const sneak = require('../index');
const sneak = require('../src/index');

describe('sneak', () => {
const defaultKey = sneak.generateKey(100);
Expand Down
59 changes: 59 additions & 0 deletions src/dh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const bignum = require('bignum');
const logger = require('logger');
const readline = require('readline');

let getSecretIter = null;
let textIter = null;

const ask = query =>
rl.question(query, answer => {
const n = bignum(answer);

if (n) {
getSecretIter.next([
n,
textIter.next().value
]);
}
});

function* getSecret(baseQuestion) {
const [g, modulusQuestion] = yield ask(baseQuestion);
const [p, secretQuestion] = yield ask(modulusQuestion);
const [exp, shareResultText] = yield ask(secretQuestion);
const bobResult = g.pow(exp).mod(p);
logger.info(`${shareResultText}${bobResult}`);
const [aliceResult, completionText] = yield ask(textIter.next().value);
const key = aliceResult.pow(exp).mod(p);
logger.success(`${completionText}${key}`);
rl.close();
}

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

const text = new Set([
'Enter the base number `g`:',
'Enter the modulus `p` (should be a sufficiently large prime):\n',
'Enter your secret exponent:\n',
'Share this result with your friend:\n',
'Enter your friend\'s result:\n',
'Use the following symmetric key for your secret communication:\n'
]);

module.exports = () => (
// textIter = text[Symbol.iterator]();
textIter = text.values(),

// Start the generators...GO!
getSecretIter = getSecret(textIter.next().value),
getSecretIter.next(),

// TODO: How to prevent the function return value from being logged to stdout.
''
);

19 changes: 10 additions & 9 deletions index.js → src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

const bignum = require('bignum');
const crypto = require('crypto');
const dh = require('./dh');
let sharedKey = null;

const convertKey = key => {
Expand All @@ -32,10 +33,7 @@ const decode = (msg, key) => {

return toDecode.split(' ')
.map(c => String.fromCharCode(
bignum.xor(
c,
secret
)
bignum.xor(c, secret)
))
.join('');
};
Expand All @@ -44,11 +42,9 @@ const encode = (msg, key) => {
const secret = convertKey(key || sharedKey);

const encoded = msg.split('')
.map(c => bignum.xor(
c.charCodeAt(),
secret
))
.join(' ');
.map(c =>
bignum.xor(c.charCodeAt(), secret)
).join(' ');

return new Buffer(encoded, 'utf8').toString('base64');
};
Expand All @@ -69,6 +65,9 @@ const generateKey = n => {
return bignum(res.concat(generateKey(--n)).join(''));
};

const generatePrime = n =>
bignum.prime(n || 512);

const getKey = () =>
sharedKey;

Expand All @@ -79,7 +78,9 @@ const setKey = key => {
module.exports = {
decode,
encode,
generateDHKey: dh,
generateKey,
generatePrime,
getKey,
setKey
};
Expand Down

0 comments on commit ab3cf05

Please sign in to comment.