rename "rand" to more explanatory "getRandomSeed"

This commit is contained in:
Ryan X. Charles 2014-06-05 16:41:47 -07:00
parent 5172b3160b
commit 80ccaa8396
3 changed files with 7 additions and 7 deletions

View File

@ -185,7 +185,7 @@ module.exports=require('6tXgqr');
},{"../common/SecureRandom":68,"buffer":83,"soop":128}],"./lib/SecureRandom":[function(require,module,exports){
module.exports=require('p4SiC2');
},{}],67:[function(require,module,exports){
(function(e){"use strict";var r=require("soop").imports(),t=r.coinUtil||require("../../util"),i=r.Point||require("../Point"),n=r.SecureRandom||require("../SecureRandom"),c=r.Key||require("../Key"),s=function(){};s.encryptObj=function(e,r,t,i){var c=new s;c.KB=e,c.message=r,t=c.r=c.rand(t);var o=(c.R,c.S=c.getSfromPubkey()),u=s.kdf(o),p=c.kE=u.slice(0,32),a=c.kM=u.slice(32,64);i=i||n.getRandomBuffer(16);{var f=c.c=s.symmetricEncrypt(p,i,r);c.d=s.mac(a,f)}return c},s.encrypt=function(r,t,i,n){var o=s.encryptObj(r,t,i,n),u=new c;u.compressed=!1,u.public=o.R.toUncompressedPubKey(),u.compressed=!0;var p=u.public,a=e.concat([p,o.c,o.d]);return a},s.decryptObj=function(e){var r=e.kB,t=e.R,n=e.c,c=e.d,o=i.multiply(t,r),u=o.x.toBuffer({size:32}),p=s.kdf(u),a=e.kE=p.slice(0,32),f=e.kM=p.slice(32,64),y=s.mac(f,n);if(c.toString("hex")!==y.toString("hex"))throw new Error("MAC check incorrect. Data is invalid.");var h=s.symmetricDecrypt(a,n);return h},s.decrypt=function(e,r){if(r.length<97)throw new Error("invalid length of encrypted data");var t=new s;t.kB=e;var n=r.slice(0,33),o=new c;return o.public=n,o.compressed=!1,t.R=i.fromUncompressedPubKey(o.public),t.c=r.slice(33,r.length-64),t.d=r.slice(r.length-64,r.length),s.decryptObj(t)},s.kdf=function(e){var r=t.sha512(e);return r},s.mac=function(e,r){var i=t.sha512hmac(e,r);return i},s.prototype.rand=function(e){return e?(this.key=new c,this.key.private=e,this.key.regenerateSync()):this.key=c.generateSync(),this.r=this.key.private,this.key.compressed=!1,this.R=i.fromUncompressedPubKey(this.key.public),this.r},s.prototype.getSfromPubkey=function(){var e=new c;e.public=this.KB,e.compressed=!1;var r=i.fromUncompressedPubKey(e.public);return this.P=i.multiply(r,this.r),this.S=this.P.x.toBuffer({size:32}),this.S},s.prototype.getSfromPrivkey=function(){var e=this.R,r=this.kB,t=i.multiply(e,r),n=t.x.toBuffer({size:32});return n},module.exports=require("soop")(s)}).call(this,require("buffer").Buffer);
(function(e){"use strict";var r=require("soop").imports(),t=r.coinUtil||require("../../util"),i=r.Point||require("../Point"),n=r.SecureRandom||require("../SecureRandom"),c=r.Key||require("../Key"),o=function(){};o.encryptObj=function(e,r,t,i){var c=new o;c.KB=e,c.message=r,t=c.getRandomSeed(t);var s=(c.R,c.S=c.getSfromPubkey()),u=o.kdf(s),p=c.kE=u.slice(0,32),a=c.kM=u.slice(32,64);i=i||n.getRandomBuffer(16);{var f=c.c=o.symmetricEncrypt(p,i,r);c.d=o.mac(a,f)}return c},o.encrypt=function(r,t,i,n){var s=o.encryptObj(r,t,i,n),u=new c;u.compressed=!1,u.public=s.R.toUncompressedPubKey(),u.compressed=!0;var p=u.public,a=e.concat([p,s.c,s.d]);return a},o.decryptObj=function(e){var r=e.kB,t=e.R,n=e.c,c=e.d,s=i.multiply(t,r),u=s.x.toBuffer({size:32}),p=o.kdf(u),a=e.kE=p.slice(0,32),f=e.kM=p.slice(32,64),y=o.mac(f,n);if(c.toString("hex")!==y.toString("hex"))throw new Error("MAC check incorrect. Data is invalid.");var m=o.symmetricDecrypt(a,n);return m},o.decrypt=function(e,r){if(r.length<97)throw new Error("invalid length of encrypted data");var t=new o;t.kB=e;var n=r.slice(0,33),s=new c;return s.public=n,s.compressed=!1,t.R=i.fromUncompressedPubKey(s.public),t.c=r.slice(33,r.length-64),t.d=r.slice(r.length-64,r.length),o.decryptObj(t)},o.kdf=function(e){var r=t.sha512(e);return r},o.mac=function(e,r){var i=t.sha512hmac(e,r);return i},o.prototype.getRandomSeed=function(e){return e?(this.key=new c,this.key.private=e,this.key.regenerateSync()):this.key=c.generateSync(),this.r=this.key.private,this.key.compressed=!1,this.R=i.fromUncompressedPubKey(this.key.public),this.r},o.prototype.getSfromPubkey=function(){var e=new c;e.public=this.KB,e.compressed=!1;var r=i.fromUncompressedPubKey(e.public);return this.P=i.multiply(r,this.r),this.S=this.P.x.toBuffer({size:32}),this.S},o.prototype.getSfromPrivkey=function(){var e=this.R,r=this.kB,t=i.multiply(e,r),n=t.x.toBuffer({size:32});return n},module.exports=require("soop")(o)}).call(this,require("buffer").Buffer);
},{"../../util":142,"../Key":"ALJ4PS","../Point":"6tXgqr","../SecureRandom":"p4SiC2","buffer":83,"soop":128}],68:[function(require,module,exports){
(function(e){var o=(require("soop"),function(){});o.getRandomBuffer=function(){},o.getPseudoRandomBuffer=function(o){for(var f=4294967296,u=new e(o),n=0;o>=n;n++){var t=Math.floor(n/4),a=n-4*t;0==a?(r=Math.random()*f,u[n]=255&r):u[n]=255&(r>>>=8)}return u},module.exports=require("soop")(o)}).call(this,require("buffer").Buffer);
},{"buffer":83,"soop":128}],"ULNIu2":[function(require,module,exports){

View File

@ -13,7 +13,7 @@ ECIES.encryptObj = function(pubkey, message, r, iv) {
var ecies = new ECIES();
ecies.KB = pubkey;
ecies.message = message;
r = ecies.r = ecies.rand(r);
r = ecies.getRandomSeed(r);
var R = ecies.R;
var S = ecies.S = ecies.getSfromPubkey();
var buf = ECIES.kdf(S);
@ -78,7 +78,7 @@ ECIES.mac = function(data, key) {
return buf;
};
ECIES.prototype.rand = function(r) {
ECIES.prototype.getRandomSeed = function(r) {
if (r) {
this.key = new Key();
this.key.private = r;

View File

@ -9,20 +9,20 @@ var Point = bitcore.Point;
describe('ECIES', function() {
describe('#rand', function() {
describe('#getRandomSeed', function() {
it('should set r and R', function() {
var ecies = new ECIES();
ecies.rand();
ecies.getRandomSeed();
ecies.r.length.should.equal(32);
ecies.R.toUncompressedPubKey().length.should.equal(65);
});
it('should not set the same r twice in a row', function() {
var ecies = new ECIES();
ecies.rand();
ecies.getRandomSeed();
var ecies2 = new ECIES();
ecies2.rand();
ecies2.getRandomSeed();
ecies.r.toString('hex').should.not.equal(ecies2.r.toString('hex'));
});