1
0
mirror of https://github.com/moparisthebest/mail synced 2025-02-07 02:20:14 -05:00

cleanu pgp code

This commit is contained in:
Tankred Hase 2013-10-11 21:54:43 +02:00
parent 5666671d2f
commit 95f815de91
2 changed files with 42 additions and 6 deletions

View File

@ -5,8 +5,7 @@ define(function(require) {
'use strict'; 'use strict';
var openpgp = require('openpgp').openpgp, var openpgp = require('openpgp').openpgp,
openpgpUtil = require('openpgp').util, util = require('openpgp').util;
util = require('cryptoLib/util');
var PGP = function() { var PGP = function() {
openpgp.init(); openpgp.init();
@ -18,7 +17,7 @@ define(function(require) {
PGP.prototype.generateKeys = function(options, callback) { PGP.prototype.generateKeys = function(options, callback) {
var userId, keys; var userId, keys;
if (!util.validateEmailAddress(options.emailAddress) || !options.keySize || typeof options.passphrase !== 'string') { if (!util.emailRegEx.test(options.emailAddress) || !options.keySize || typeof options.passphrase !== 'string') {
callback({ callback({
errMsg: 'Crypto init failed. Not all options set!' errMsg: 'Crypto init failed. Not all options set!'
}); });
@ -30,7 +29,7 @@ define(function(require) {
keys = openpgp.generate_key_pair(1, options.keySize, userId, options.passphrase); keys = openpgp.generate_key_pair(1, options.keySize, userId, options.passphrase);
callback(null, { callback(null, {
keyId: openpgpUtil.hexstrdump(keys.privateKey.getKeyId()).toUpperCase(), keyId: util.hexstrdump(keys.privateKey.getKeyId()).toUpperCase(),
privateKeyArmored: keys.privateKeyArmored, privateKeyArmored: keys.privateKeyArmored,
publicKeyArmored: keys.publicKeyArmored publicKeyArmored: keys.publicKeyArmored
}); });
@ -40,6 +39,8 @@ define(function(require) {
* Import the user's key pair * Import the user's key pair
*/ */
PGP.prototype.importKeys = function(options, callback) { PGP.prototype.importKeys = function(options, callback) {
var publicKey, privateKey;
// check passphrase // check passphrase
if (typeof options.passphrase !== 'string' || !options.privateKeyArmored || !options.publicKeyArmored) { if (typeof options.passphrase !== 'string' || !options.privateKeyArmored || !options.publicKeyArmored) {
callback({ callback({
@ -57,6 +58,19 @@ define(function(require) {
} }
// import public key // import public key
openpgp.keyring.importPublicKey(options.publicKeyArmored); openpgp.keyring.importPublicKey(options.publicKeyArmored);
// check if keys have the same id
privateKey = openpgp.keyring.exportPrivateKey(0);
publicKey = openpgp.keyring.getPublicKeysForKeyId(privateKey.keyId)[0];
if (!privateKey || !privateKey.armored || !publicKey || !publicKey.armored || privateKey.keyId !== publicKey.keyId) {
// reset keyring
openpgp.keyring.init();
callback({
errMsg: 'Key IDs dont match!'
});
return;
}
callback(); callback();
}; };
@ -71,7 +85,7 @@ define(function(require) {
if (privateKey && privateKey.keyId && privateKey.armored && publicKey && publicKey.armored) { if (privateKey && privateKey.keyId && privateKey.armored && publicKey && publicKey.armored) {
callback(null, { callback(null, {
keyId: openpgpUtil.hexstrdump(privateKey.keyId).toUpperCase(), keyId: util.hexstrdump(privateKey.keyId).toUpperCase(),
privateKeyArmored: privateKey.armored, privateKeyArmored: privateKey.armored,
publicKeyArmored: publicKey.armored publicKeyArmored: publicKey.armored
}); });

View File

@ -6,7 +6,7 @@ define(function(require) {
describe('PGP Crypto Api unit tests', function() { describe('PGP Crypto Api unit tests', function() {
var pgp, var pgp,
user = "whiteout.test@t-online.de", user = 'whiteout.test@t-online.de',
passphrase = 'asdf', passphrase = 'asdf',
keySize = 512, keySize = 512,
keyId = 'F6F60E9B42CDFF4C', keyId = 'F6F60E9B42CDFF4C',
@ -45,6 +45,28 @@ define(function(require) {
afterEach(function() {}); afterEach(function() {});
describe('Generate key pair', function() { describe('Generate key pair', function() {
it('should fail', function(done) {
pgp.generateKeys({
emailAddress: 'whiteout.test@t-onlinede',
keySize: keySize,
passphrase: passphrase
}, function(err, keys) {
expect(err).to.exist;
expect(keys).to.not.exist;
done();
});
});
it('should fail', function(done) {
pgp.generateKeys({
emailAddress: 'whiteout.testt-online.de',
keySize: keySize,
passphrase: passphrase
}, function(err, keys) {
expect(err).to.exist;
expect(keys).to.not.exist;
done();
});
});
it('should work', function(done) { it('should work', function(done) {
pgp.generateKeys({ pgp.generateKeys({
emailAddress: user, emailAddress: user,