RSAPublicKey.fromASN1 constructor

RSAPublicKey.fromASN1(
  1. dynamic input, {
  2. bool fromPkcs1 = false,
})

Implementation

factory RSAPublicKey.fromASN1(/* String | Iterable<int> */ input,
    {bool fromPkcs1 = false}) {
  if (!fromPkcs1) {
    final seq = ASN1Sequence.decode(input);
    if (seq.children.length != 2) {
      throw Exception('Invalid structure');
    }

    if (seq.children[1] is! ASN1BitString) {
      throw Exception('Invalid structure');
    }

    final bitString = seq.children[1] as ASN1BitString;

    if (bitString.unusedBits != 0) {
      throw Exception('Invalid structure');
    }

    if (seq.children[0] is! ASN1Sequence) {
      throw Exception('Invalid structure');
    }

    final algIdentifier = seq.children[0] as ASN1Sequence;

    if (algIdentifier.children.isEmpty) {
      throw Exception('Invalid structure');
    }

    if (algIdentifier.children.first is! ASN1ObjectIdentifier) {
      throw Exception('Invalid structure');
    }

    final ASN1ObjectIdentifier identifer =
        algIdentifier.children.first as ASN1ObjectIdentifier;

    if (identifer.objectIdentifierAsString != '1.2.840.113549.1.1.1') {
      throw Exception('Invalid structure');
    }

    input = bitString.bitString;
  }

  final seq = ASN1Sequence.decode(input);

  if (seq.children.length != 2) {
    throw Exception('Invalid structure');
  }

  if (seq.children.any((e) => e is! ASN1Integer)) {
    throw Exception('Invalid structure');
  }

  final numbers =
      seq.children.cast<ASN1Integer>().map((e) => e.value).toList();

  return RSAPublicKey(numbers[0], numbers[1]);
}