Created
June 26, 2022 09:20
-
-
Save RyanDsilva/d20ba61db26a2eba7b4d33834d6a8773 to your computer and use it in GitHub Desktop.
SSL Pinning Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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