Added SEA.keyid() to support shorter notation of public key

This commit is contained in:
mhelander 2017-09-27 12:42:33 +03:00
parent 79c703fd81
commit 01db961adb
2 changed files with 42 additions and 1 deletions

26
sea.js
View File

@ -469,12 +469,17 @@
}
});
}
// This internal func returns hashed data for signing
// This internal func returns SHA-256 hashed data for signing
function sha256hash(m){
var hashSubtle = subtleossl || subtle;
try{ m = m.slice ? m : JSON.stringify(m) }catch(e){} //eslint-disable-line no-empty
return hashSubtle.digest(pbkdf2.hash, new TextEncoder("utf-8").encode(m));
}
// This internal func returns SHA-1 hashed data for KeyID generation
function sha1hash(b){
var hashSubtle = subtleossl || subtle;
return hashSubtle.digest('SHA-1', b);
}
// How does it work?
function User(){}
@ -902,6 +907,21 @@
};
if(cb){ doIt(cb, function(){cb()}) } else { return new Promise(doIt) }
};
// Calculate public key KeyID aka PGPv4 (result: 8 bytes as hex string)
SEA.keyid = function(p,cb){
var doIt = function(resolve, reject){
// base64('base64(x):base64(y)') => Buffer(xy)
var pb = Buffer.concat((new Buffer(p, 'base64')).toString('utf8').split(':')
.map(function(t){ return new Buffer(t, 'base64') }));
// id is PGPv4 compliant raw key
var id = Buffer.concat([new Buffer([0x99, pb.length/0x100, pb.length%0x100]), pb]);
sha1hash(id).then(function(sha1){
var hash = new Buffer(sha1, 'binary');
resolve(hash.slice(hash.length-8).toString('hex')); // 16-bit ID as hex
});
};
if(cb){ doIt(cb, function(){cb()}) } else { return new Promise(doIt) }
};
SEA.pair = function(cb){
var doIt = function(resolve, reject){
// First: ECDSA keys for signing/verifying...
@ -913,6 +933,10 @@
}).then(function(keys){
return subtle.exportKey('jwk', pubkey).then(function(k){
keys.pub = (new Buffer([k.x, k.y].join(':'))).toString('base64');
// return SEA.keyid(keys.pub).then(function(id){
// keys.pubId = id;
// return keys;
// });
return keys;
});
}).catch(function(e){ Gun.log(e); reject(e) });

View File

@ -77,6 +77,23 @@ Gun.SEA && describe('SEA', function(){
}
});
it('keyid', function(done){
Gun.SEA.pair().then(function(key){
var check = function(keyid){
expect(keyid).to.not.be(undefined);
expect(keyid).to.not.be('');
expect(keyid.length).to.eql(16);
done();
};
// keyid - creates 8 byte KeyID from public key
if(type === 'callback'){
Gun.SEA.keyid(key.pub, check);
} else {
Gun.SEA.keyid(key.pub).then(check);
}
}).catch(function(e){done(e)});
});
it('enc', function(done){
Gun.SEA.pair().then(function(key){
var check = function(jsonSecret){