|
|
(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); |