Sha256: 6fb69fe3a4018457f09876d24cd075f17b0b34ef61e415ff51661cecc8d0f524

Contents?: true

Size: 1.79 KB

Versions: 29

Compression:

Stored size: 1.79 KB

Contents

var crypto = require("crypto");
var BigInteger = require("jsbn").BigInteger;
var ECPointFp = require("./lib/ec.js").ECPointFp;
var Buffer = require("safer-buffer").Buffer;
exports.ECCurves = require("./lib/sec.js");

// zero prepad
function unstupid(hex,len)
{
	return (hex.length >= len) ? hex : unstupid("0"+hex,len);
}

exports.ECKey = function(curve, key, isPublic)
{
  var priv;
	var c = curve();
	var n = c.getN();
  var bytes = Math.floor(n.bitLength()/8);

  if(key)
  {
    if(isPublic)
    {
      var curve = c.getCurve();
//      var x = key.slice(1,bytes+1); // skip the 04 for uncompressed format
//      var y = key.slice(bytes+1);
//      this.P = new ECPointFp(curve,
//        curve.fromBigInteger(new BigInteger(x.toString("hex"), 16)),
//        curve.fromBigInteger(new BigInteger(y.toString("hex"), 16)));      
      this.P = curve.decodePointHex(key.toString("hex"));
    }else{
      if(key.length != bytes) return false;
      priv = new BigInteger(key.toString("hex"), 16);      
    }
  }else{
    var n1 = n.subtract(BigInteger.ONE);
    var r = new BigInteger(crypto.randomBytes(n.bitLength()));
    priv = r.mod(n1).add(BigInteger.ONE);
    this.P = c.getG().multiply(priv);
  }
  if(this.P)
  {
//  var pubhex = unstupid(this.P.getX().toBigInteger().toString(16),bytes*2)+unstupid(this.P.getY().toBigInteger().toString(16),bytes*2);
//  this.PublicKey = Buffer.from("04"+pubhex,"hex");
    this.PublicKey = Buffer.from(c.getCurve().encodeCompressedPointHex(this.P),"hex");
  }
  if(priv)
  {
    this.PrivateKey = Buffer.from(unstupid(priv.toString(16),bytes*2),"hex");
    this.deriveSharedSecret = function(key)
    {
      if(!key || !key.P) return false;
      var S = key.P.multiply(priv);
      return Buffer.from(unstupid(S.getX().toBigInteger().toString(16),bytes*2),"hex");
   }     
  }
}

Version data entries

29 entries across 28 versions & 12 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/ecc-jsbn/index.js
disco_app-0.18.0 test/dummy/node_modules/ecc-jsbn/index.js
disco_app-0.18.2 test/dummy/node_modules/ecc-jsbn/index.js
tang-0.2.1 spec/tang_app/node_modules/ecc-jsbn/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/ecc-jsbn/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/ecc-jsbn/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/ecc-jsbn/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/ecc-jsbn/index.js
tang-0.2.0 spec/tang_app/node_modules/ecc-jsbn/index.js
tang-0.1.0 spec/tang_app/node_modules/ecc-jsbn/index.js
tang-0.0.9 spec/tang_app/node_modules/ecc-jsbn/index.js
enju_library-0.3.8 spec/dummy/node_modules/ecc-jsbn/index.js
ilog-0.4.1 node_modules/ecc-jsbn/index.js
ilog-0.4.0 node_modules/ecc-jsbn/index.js
ilog-0.3.3 node_modules/ecc-jsbn/index.js
learn_create-0.0.22 lib/templates/javascript_lab_template/node_modules/ecc-jsbn/index.js
jester-data-8.0.0 node_modules/ecc-jsbn/index.js
ezii-os-5.2.1 node_modules/ecc-jsbn/index.js
ezii-os-2.0.1 node_modules/ecc-jsbn/index.js
ezii-os-1.1.0 node_modules/ecc-jsbn/index.js