getCertificateInfoFromUrl function

Future<CertificateInformation?> getCertificateInfoFromUrl(
  1. String url
)

Request TLS Certificate of url and extract important information from it.

Implementation

Future<CertificateInformation?> getCertificateInfoFromUrl(String url) async {
  if (!url.startsWith(RegExp('http.', caseSensitive: false))) {
    url = 'https://$url';
  }
  if (!url.startsWith(RegExp('https', caseSensitive: false))) {
    url = url.replaceAll(RegExp('http', caseSensitive: false), 'https');
  }
  try {
    var client = await HttpClient()
        .getUrl(Uri.parse(url))
        .timeout(Duration(seconds: 30));
    var res = await client.close();
    if (res.certificate == null) {
      return null;
    } else {
      return CertificateInformation(res.certificate, true);
    }
  } catch (e) {
    throw Exception('Error occurred during certificate check: $e');
  }
}