Skip to content

Instantly share code, notes, and snippets.

@RyanDsilva
Created June 26, 2022 09:20
Show Gist options
  • Save RyanDsilva/d20ba61db26a2eba7b4d33834d6a8773 to your computer and use it in GitHub Desktop.
Save RyanDsilva/d20ba61db26a2eba7b4d33834d6a8773 to your computer and use it in GitHub Desktop.
SSL Pinning Implementation
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/io_client.dart';
Future<SecurityContext> get globalContext async {
final sslCert = await rootBundle.load('assets/certs/ryandsilva-dev.pem');
final sc = SecurityContext(withTrustedRoots: false);
sc.setTrustedCertificatesBytes(sslCert.buffer.asInt8List());
return sc;
}
Future<bool> verifyHandshake(String url) async {
try {
final client = HttpClient(context: await globalContext);
client.badCertificateCallback = ((cert, host, port) => false);
final ioClient = IOClient(client);
await ioClient.get(Uri.parse(url));
return true;
} catch (e) {
debugPrint('Could not complete SSL handshake: ${e.toString()}');
return false;
}
}
bool isSecureConnection = await verifyHandshake('https://ryandsilva.dev/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment