toKeyPair method

KeyPair toKeyPair()

Implementation

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

    case 'OCP':
      if (crv == 'Ed25519') {
        final y = this.y!;
        return SimpleKeyPair.lazy(
          () async {
            return Ed25519().newKeyPairFromSeed(y);
          },
        );
      }
      if (crv == 'X25519') {
        final y = this.y!;
        return SimpleKeyPair.lazy(
          () async {
            return X25519().newKeyPairFromSeed(y);
          },
        );
      }
      throw StateError('Unsupported "crv": "$crv"');

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

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