fromKeyPair static method

Jwk fromKeyPair(
  1. KeyPair keyPair
)

Implementation

static Jwk fromKeyPair(KeyPair keyPair) {
  if (keyPair is EcKeyPairData) {
    final crv = <KeyPairType, String>{
      KeyPairType.p256: 'P-256',
      KeyPairType.p384: 'P-384',
      KeyPairType.p521: 'P-521',
    }[keyPair.type];
    if (crv != null) {
      return Jwk(
        kty: 'EC',
        crv: crv,
        d: keyPair.d,
        x: keyPair.x,
        y: keyPair.y,
      );
    }
  } else if (keyPair is SimpleKeyPairData) {
    final crv = const <KeyPairType, String>{
      KeyPairType.ed25519: 'Ed25519',
      KeyPairType.x25519: 'X25519',
    }[keyPair.type];
    if (crv != null) {
      return Jwk(
        kty: 'EC',
        crv: crv,
        x: keyPair.bytes,
      );
    }
  } else if (keyPair is RsaKeyPairData) {
    return Jwk(
      kty: 'RSA',
      e: keyPair.e,
      d: keyPair.d,
      dp: keyPair.dp,
      dq: keyPair.dq,
      n: keyPair.n,
      p: keyPair.p,
      q: keyPair.q,
      qi: keyPair.qi,
    );
  }
  throw ArgumentError.value(keyPair);
}