parent
c13f5c9840
commit
324844eb25
4 changed files with 102 additions and 56 deletions
@ -0,0 +1,83 @@ |
||||
module.exports = function(chatserver, keyserver) { |
||||
|
||||
var package = require(__dirname+'/../package.json') |
||||
var config = require(package.path.config) |
||||
var io = require('socket.io-client') |
||||
var openpgp = require('openpgp') |
||||
openpgp.initWorker() |
||||
openpgp.config.aead_protect = true |
||||
|
||||
var module = function() { |
||||
var _hkp = new openpgp.HKP(keyserver) |
||||
var _user = null |
||||
|
||||
this.login = function(user, password, success, fail) { |
||||
_user = user |
||||
_hkp.lookup({query: _user.email}).then(function(key) { |
||||
var data = (new Date()).toLocaleString()+' '+_user.name+' '+_user.email |
||||
console.log('data:', data) |
||||
var pubkeys = openpgp.key.readArmored(key) |
||||
var privkey = openpgp.key.readArmored(_user.key.priv).keys[0] |
||||
privkey.decrypt(password) |
||||
if (pubkeys.keys.length==1) { |
||||
openpgp.encrypt({ |
||||
data: data, |
||||
publicKeys: pubkeys.keys[0], |
||||
privateKeys: privkey |
||||
}).then(function(ciphertext) { |
||||
openpgp.decrypt({ |
||||
message: ciphertext, |
||||
publicKeys: openpgp.key.readArmored(_user.key.pub).keys[0], |
||||
privateKey: privkey |
||||
}).then(function(plaintext) { |
||||
if (data==plaintext) { |
||||
success(_user) |
||||
} else { |
||||
fail('local key does not match key on server') |
||||
} |
||||
}).catch(function(e) { |
||||
fail('decryption failed', e) |
||||
}) |
||||
}).catch(function(e) { |
||||
fail('encryption failed', e) |
||||
}) |
||||
} else { |
||||
fail('more than one public key on server', pubkeys.keys) |
||||
} |
||||
}).catch(function(e) { |
||||
fail('check key on server failed', e) |
||||
}) |
||||
} |
||||
|
||||
this.create = function(name, host, password, success, fail) { |
||||
var _this = this |
||||
openpgp.generateKey({ |
||||
numBits: 4096, |
||||
userIds: [{name: name, email: name+'@'+host}], |
||||
passphrase: password |
||||
}).then(function(key) { |
||||
_user = { |
||||
name: name, |
||||
email: name+'@'+host, |
||||
numBits: 4096, |
||||
key: { |
||||
pub: key.publicKeyArmored, |
||||
priv: key.privateKeyArmored |
||||
} |
||||
} |
||||
_hkp.upload(_user.key.pub).then(function() { |
||||
console.log('key successfully uploaded') |
||||
_this.login(_user, password, success, fail) |
||||
}).catch(function(e) { |
||||
fail('upload key failed', e) |
||||
}) |
||||
}).catch(function(e) { |
||||
fail('generating key pairs failed', e) |
||||
}) |
||||
} |
||||
|
||||
} |
||||
|
||||
return new module(); |
||||
|
||||
} |
Loading…
Reference in new issue