KeyPair.fromJwk constructor

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

Create a key pair from a JsonWebKey

Implementation

factory 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':
      return KeyPair(
          privateKey: jwk.containsKey('d') && jwk.containsKey('crv')
              ? EcPrivateKey(
                  eccPrivateKey: _base64ToInt(jwk['d']),
                  curve: _parseCurve(jwk['crv']))
              : null,
          publicKey: jwk.containsKey('x') &&
                  jwk.containsKey('y') &&
                  jwk.containsKey('crv')
              ? EcPublicKey(
                  xCoordinate: _base64ToInt(jwk['x']),
                  yCoordinate: _base64ToInt(jwk['y']),
                  curve: _parseCurve(jwk['crv']))
              : null);
  }
  throw ArgumentError('Unknown key type ${jwk['kty']}');
}