toKeyPair method

KeyPair toKeyPair()

Constructs a KeyPair from this Jwk.

Implementation

KeyPair toKeyPair() {
  switch (kty) {
    case 'EC':
      final type = const <String, KeyPairType>{
        'P-256': KeyPairType.p256,
        'secp256k1': KeyPairType.p256k,
        'P-384': KeyPairType.p384,
        'P-521': KeyPairType.p521,
      }[crv];
      if (type == null) {
        throw StateError('Unsupported "crv": "$crv"');
      }
      return EcKeyPairData(
        d: Uint8List.fromList(d ?? const <int>[]),
        x: Uint8List.fromList(x ?? const <int>[]),
        y: Uint8List.fromList(y ?? const <int>[]),
        type: type,
      );

    case 'OKP':
      if (crv == 'Ed25519') {
        final d = this.d!;
        return _LazySimpleKeyPair(
          d,
          () async {
            final keyPair = await Ed25519().newKeyPairFromSeed(d);
            return await keyPair.extractPublicKey();
          },
          KeyPairType.ed25519,
        );
      }
      if (crv == 'X25519') {
        final d = this.d!;
        return _LazySimpleKeyPair(
          d,
          () async {
            final keyPair = await X25519().newKeyPairFromSeed(d);
            return await keyPair.extractPublicKey();
          },
          KeyPairType.x25519,
        );
      }
      throw StateError('Unsupported "crv": "$crv"');

    case 'RSA':
      return RsaKeyPairData(
        e: Uint8List.fromList(e ?? const <int>[]),
        d: Uint8List.fromList(d ?? const <int>[]),
        dp: Uint8List.fromList(dp ?? const <int>[]),
        dq: Uint8List.fromList(dq ?? const <int>[]),
        n: Uint8List.fromList(n ?? const <int>[]),
        p: Uint8List.fromList(p ?? const <int>[]),
        q: Uint8List.fromList(q ?? const <int>[]),
        qi: Uint8List.fromList(qi ?? const <int>[]),
      );

    default:
      throw StateError('Not a key pair (kty: $kty)');
  }
}