0.18.0 • Published 8 years ago

ec-pem v0.18.0

Weekly downloads
493
License
ISC
Repository
github
Last release
8 years ago

ec_pem

Enables crypto.sign and crypto.verify using crypto.createECDH generated keys

Use

const crypto = require('crypto');
const ec_pem = require('ec-pem');

let alice_pem_private, alice_pem_public;
{
  let curve = 'secp521r1'
  let alice = crypto.createECDH(curve);
  alice.generateKeys();
  alice = ec_pem(alice, curve);

  alice_pem_private = alice.encodePrivateKey();
  // console.log(alice_pem_private);

  alice_pem_public = alice.encodePublicKey();
  // console.log(alice_pem_public);
}


const data = Buffer.from('a message to sign');

let signature;
{
  const sign = crypto.createSign('sha256');
  sign.update(data);
  signature = sign.sign(alice_pem_private);
  // console.log({signature})
}


{
  const verify = crypto.createVerify('sha256');
  verify.update(data);
  const matched = verify.verify(alice_pem_public, signature);
  // console.log({matched})
  if (!matched)
    throw new Error("Verification failed");
}

Use ec_pem/cert

const https = require('https');
const ec_pem = require('ec-pem');
const ec_cert = require('ec-pem/cert');

const tls_options = ec_cert.createSelfSignedCertificate('example.com', 
    { ec: ec_pem.generate('prime256v1'),
      days: 30, altNames: ['example.com', 'www.example.com', '1.234.56.255'] })

demo_https_server(tls_options)



function demo_https_server(tls_options) {
  let svr = tls_options
    .then(options => https.createServer(options) )
    .then(svr => {
      svr.on('secureConnection', sock => console.log("New secure connection"));
      svr.on('request', (req,res) => {
        res.writeHead(200);
        res.end('hello world\n');
      });
      return svr })
    .then(svr => new Promise((resolve, reject) =>
        svr.listen(8443, '127.0.0.1',
          (err) => err ? reject(err) : resolve(svr))))

  svr.then(() =>
    https.get(
      {hostname: '127.0.0.1', port:8443, pathname:'/', rejectUnauthorized: false},
      res => { console.log(res.statusCode) }))
}

Use ec_pem/local_cert

const https = require('https');
const ec_localcert = require('ec-pem/local_cert');

const tls_options = ec_localcert('tls_local.json')

demo_https_server(tls_options)

or

const https = require('https');
const ec_localcert = require('ec-pem/local_cert');

const tls_options = ec_localcert('tls_local.json', ({ec_cert, ec_pem}) =>
  ec_cert.createSelfSignedCertificate('localhost.dev', {
    ec: ec_pem.generate('prime256v1'),
    days: 3660, // 10 years
    altNames: ['localhost.dev', 'localhost', '127.0.0.1'],
  }))

demo_https_server(tls_options)
0.18.0

8 years ago

0.17.1

8 years ago

0.17.0

9 years ago

0.16.0

9 years ago

0.15.2

9 years ago

0.15.1

9 years ago

0.15.0

9 years ago

0.14.2

9 years ago

0.14.1

9 years ago

0.14.0

9 years ago

0.13.1

9 years ago

0.13.0

9 years ago

0.12.2

9 years ago

0.12.1

9 years ago

0.12.0

9 years ago

0.11.3

9 years ago

0.11.2

9 years ago

0.11.1

9 years ago

0.11.0

9 years ago

0.10.1

9 years ago

0.10.0

9 years ago

0.9.2

9 years ago

0.9.1

9 years ago

0.9.0

9 years ago

0.8.4

10 years ago

0.8.2

10 years ago

0.8.1

10 years ago

0.8.0

10 years ago

0.7.0

10 years ago

0.6.2

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago