async function toJwk(key, format, {outputPublic, passphrase}){
  // Peel the pem strings
  const binKey = (format === 'pem') ? jseu.formatter.pemToBin(key) : key;

  // decode binary spki/pkcs8-formatted key to parsed object
  let decoded;
  try { decoded = KeyStructure.decode(Buffer.from(binKey), 'der'); }
  catch (e) { throw new Error('FailedToDecodeKey'); }

  let type;
  if(decoded.type === 'subjectPublicKeyInfo'){
    type = 'public';
    decoded = decoded.value;
  }
  else {
    type = (typeof outputPublic === 'boolean' && outputPublic) ? 'public' : 'private';

    if(decoded.type === 'encryptedPrivateKeyInfo') decoded = await decryptEncryptedPrivateKeyInfo(decoded.value, passphrase);
    else if (decoded.type === 'oneAsymmetricKey') decoded = decoded.value;
    else throw new Error('UnsupportedKeyStructure');
  }

  const keyTypes = getAlgorithmFromOid(
    (type === 'public') ? decoded.algorithm.algorithm : decoded.privateKeyAlgorithm.algorithm,
    params.publicKeyAlgorithms
  );
  if(keyTypes.length < 1) throw new Error('UnsupportedKey');

  if (keyTypes[0] === 'EC') {
    return asn1ec.toJWK(decoded, type);
  }
  else if (keyTypes[0] === 'RSA'){
    return asn1rsa.toJwk(decoded, type);
  }
  else throw new Error('InvalidKeyType');
}
/**
 * Convert spki/pkcs8 key in string or binary format to jwk.
 * @param key
 * @param format
 * @param outputPublic {boolean} (optional)
 * @param passphrase
 * @return {*|void}
 */
Convert spki/pkcs8 key in string or binary format to jwk.
Convert spki / pkcs8 key in string or binary format to jwk .
async function toJwk(key, format, {outputPublic, passphrase}){
  const binKey = (format === 'pem') ? jseu.formatter.pemToBin(key) : key;
  let decoded;
  try { decoded = KeyStructure.decode(Buffer.from(binKey), 'der'); }
  catch (e) { throw new Error('FailedToDecodeKey'); }
  let type;
  if(decoded.type === 'subjectPublicKeyInfo'){
    type = 'public';
    decoded = decoded.value;
  }
  else {
    type = (typeof outputPublic === 'boolean' && outputPublic) ? 'public' : 'private';
    if(decoded.type === 'encryptedPrivateKeyInfo') decoded = await decryptEncryptedPrivateKeyInfo(decoded.value, passphrase);
    else if (decoded.type === 'oneAsymmetricKey') decoded = decoded.value;
    else throw new Error('UnsupportedKeyStructure');
  }
  const keyTypes = getAlgorithmFromOid(
    (type === 'public') ? decoded.algorithm.algorithm : decoded.privateKeyAlgorithm.algorithm,
    params.publicKeyAlgorithms
  );
  if(keyTypes.length < 1) throw new Error('UnsupportedKey');
  if (keyTypes[0] === 'EC') {
    return asn1ec.toJWK(decoded, type);
  }
  else if (keyTypes[0] === 'RSA'){
    return asn1rsa.toJwk(decoded, type);
  }
  else throw new Error('InvalidKeyType');
}
async function toJwk ( key , format , { outputPublic , passphrase } ) { const binKey = ( format === 'pem' ) ? jseu . formatter . pemToBin ( key ) : key ; let decoded ; try { decoded = KeyStructure . decode ( Buffer . from ( binKey ) , 'der' ) ; } catch ( e ) { throw new Error ( 'FailedToDecodeKey' ) ; } let type ; if ( decoded . type === 'subjectPublicKeyInfo' ) { type = 'public' ; decoded = decoded . value ; } else { type = ( typeof outputPublic === 'boolean' && outputPublic ) ? 'public' : 'private' ; if ( decoded . type === 'encryptedPrivateKeyInfo' ) decoded = await decryptEncryptedPrivateKeyInfo ( decoded . value , passphrase ) ; else if ( decoded . type === 'oneAsymmetricKey' ) decoded = decoded . value ; else throw new Error ( 'UnsupportedKeyStructure' ) ; } const keyTypes = getAlgorithmFromOid ( ( type === 'public' ) ? decoded . algorithm . algorithm : decoded . privateKeyAlgorithm . algorithm , params . publicKeyAlgorithms ) ; if ( keyTypes . length < 1 ) throw new Error ( 'UnsupportedKey' ) ; if ( keyTypes [ 0 ] === 'EC' ) { return asn1ec . toJWK ( decoded , type ) ; } else if ( keyTypes [ 0 ] === 'RSA' ) { return asn1rsa . toJwk ( decoded , type ) ; } else throw new Error ( 'InvalidKeyType' ) ; }
Convert spki/pkcs8 key in string or binary format to jwk.
Convert spki / pkcs8 key in string or binary format to jwk .
// Peel the pem strings // decode binary spki/pkcs8-formatted key to parsed object