fromJwk static method

KeyPair? fromJwk(
  1. Map<String, dynamic> jwk
)

Create a key pair from a JsonWebKey

Implementation

static KeyPair? fromJwk(Map<String, dynamic> jwk) {
  switch (jwk['kty']) {
    case 'oct':
      var key = SymmetricKey(keyValue: _base64ToBytes(jwk['k']) as Uint8List);
      return KeyPair(publicKey: key, privateKey: key);
    case 'RSA':
      return KeyPair(
          publicKey: jwk.containsKey('n') && jwk.containsKey('e')
              ? RsaPublicKey(
                  modulus: _base64ToInt(jwk['n']),
                  exponent: _base64ToInt(jwk['e']),
                )
              : null,
          privateKey: jwk.containsKey('n') &&
                  jwk.containsKey('d') &&
                  jwk.containsKey('p') &&
                  jwk.containsKey('q')
              ? RsaPrivateKey(
                  modulus: _base64ToInt(jwk['n']),
                  privateExponent: _base64ToInt(jwk['d']),
                  firstPrimeFactor: _base64ToInt(jwk['p']),
                  secondPrimeFactor: _base64ToInt(jwk['q']),
                )
              : null);
    case 'EC':
      final curve = _parseCurve(jwk['crv']);
      return KeyPair(
        privateKey: jwk.containsKey('d') && curve != null
            ? EcPrivateKey(
                eccPrivateKey: _base64ToInt(jwk['d']),
                curve: curve,
              )
            : null,
        publicKey:
            jwk.containsKey('x') && jwk.containsKey('y') && curve != null
                ? EcPublicKey(
                    xCoordinate: _base64ToInt(jwk['x']),
                    yCoordinate: _base64ToInt(jwk['y']),
                    curve: curve,
                  )
                : null,
      );
  }
  return null;
}