DEV Community

Zaw Htut Win
Zaw Htut Win

Posted on

MyInfo v4 cert and JWKS

This is the code


const jose = require('node-jose');
const crypto = require('crypto');
async function generateKey(){
  let key = crypto.generateKeyPairSync('ec', {
    namedCurve: 'prime256v1',
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem',
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem',
    },
  });
  let cryptoKey = await jose.JWK.asKey(key.privateKey, 'pem');
  console.log(cryptoKey.toPEM(true));
  return cryptoKey;
}
async function generateJwks() {
  //Creating Signing Key
  let signingKey = await generateKey();
  let publicSigningKeyJSON = signingKey.toJSON();

  //Creating Encryption Key
  let encryptionKey = await generateKey();
  let publicEncryptionKeyJSON = encryptionKey.toJSON();

    let jwks = {
      keys: [{...publicSigningKeyJSON,
              ...{use: 'sig'},
              ...{crv: 'P-256'},
              ...{alg: 'ES256'},
    },
    {...publicEncryptionKeyJSON,
              ...{use: 'enc'},
              ...{crv: 'P-256'},
              ...{alg: 'ECDH-ES+A256KW'},
    }]};

    console.log(JSON.stringify(jwks));
}
generateJwks();
Enter fullscreen mode Exit fullscreen mode

and this is the sample output

-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIMQNi+nu+SAsdoC/6+EX9uqt6AsHqfRjBfXe9zMwixHJoAoGCCqGSM49
AwEHoUQDQgAEJjjlODeUwHh4jULfxMAYaY2UrdKNanlfdLJUeSTlTGehI6Buu/Iu
keVK2A4iZLsisVgYDzos0YpTrO+EYW+XuA==
-----END EC PRIVATE KEY-----

-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIDr+f2B3/vXJ77aoq8fVmbe6/0OfzX3TrbRdfa6fbFq8oAoGCCqGSM49
AwEHoUQDQgAEPDdhVbX9BcS06GkUSe+g+C5TVARhaSuk3Gm4n9uaqdKw00EjYGIV
54WYSZlbKNxWULy10rp6VbFSjHn4U/X80w==
-----END EC PRIVATE KEY-----

{"keys":[{"kty":"EC","kid":"q5oJ6FCIhF9bV0rjV6jjF3EvEjEODDC2n7rm6hLyDWQ","crv":"P-256","x":"JjjlODeUwHh4jULfxMAYaY2UrdKNanlfdLJUeSTlTGc","y":"oSOgbrvyLpHlStgOImS7IrFYGA86LNGKU6zvhGFvl7g","use":"sig","alg":"ES256"},{"kty":"EC","kid":"mP4kHc_EMpQ15sHnmWQsvRwSBzNA5kkAxBzJdFY-jHo","crv":"P-256","x":"PDdhVbX9BcS06GkUSe-g-C5TVARhaSuk3Gm4n9uaqdI","y":"sNNBI2BiFeeFmEmZWyjcVlC8tdK6elWxUox5-FP1_NM","use":"enc","alg":"ECDH-ES+A256KW"}]}
Enter fullscreen mode Exit fullscreen mode

Then you can start using them with MyInfoConnector for NodeJS lib with their sample app. Happy Coding!

Top comments (0)