connect_secure π
A Dart/Flutter package to add SSL Pinning support in your applications. It ensures that your app only communicates with trusted servers by validating server certificates or public key fingerprints.
This helps protect against MITM (Man-in-the-Middle) attacks and ensures secure communication.
β¨ Features
- β SSL Pinning with SHA-256 certificate fingerprints
- β Works with Dio and http (IOClient)
- β Host-based pinning and fingerprint normalization (colon/space-insensitive)
- β Easy to configure and integrate
- β Lightweight & customizable
π Installation
Add the dependency in your pubspec.yaml
:
dependencies:
connect_secure: ^1.0.0
Then run:
flutter pub get
π Usage
Import the package:
import 'package:connect_secure/connect_secure.dart';
Example: Using SSL Pinning with Dio
import 'package:dio/dio.dart';
import 'package:connect_secure/connect_secure.dart';
void main() async {
final dio = Dio();
// Attach SSL Pinning adapter
dio.httpClientAdapter = DioSslPinning(
allowedFingerprints: [
// Add your server's SHA-256 certificate fingerprint (colon/space format allowed)
"12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF",
],
// Optionally pin different hosts to different fingerprints
fingerprintsByHost: {
"api.your-secure-api.com": [
"12 34 56 78 90 ab cd ef 12 34 56 78 90 ab cd ef 12 34 56 78 90 ab cd ef 12 34 56 78 90 ab cd ef",
],
},
);
try {
final response = await dio.get("https://your-secure-api.com");
print("β
Response: ${response.data}");
} catch (e) {
print("β SSL Pinning validation failed: $e");
}
}
Example: Using SSL Pinning with http (IOClient)
import 'package:http/http.dart' as http;
import 'package:connect_secure/connect_secure.dart';
void main() async {
final client = createPinnedHttpClient(
allowedFingerprints: [
"12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF",
],
fingerprintsByHost: {
"example.com": ["f9b2f8d74c6f5f8e8c0b9e0d12345abcd..."],
},
);
final response = await client.get(Uri.parse('https://example.com'));
print(response.statusCode);
}
Example: Using raw dart:io HttpClient
import 'dart:convert';
import 'package:connect_secure/connect_secure.dart';
void main() async {
final httpClient = SecureHttpClient(
allowedFingerprints: [
"12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF",
],
fingerprintsByHost: {
"example.com": ["f9b2f8d74c6f5f8e8c0b9e0d12345abcd..."],
},
);
final res = await httpClient.get(Uri.parse('https://example.com'));
final body = await res.transform(utf8.decoder).join();
print(body);
}
π Example Project
See the example for a full working demo.
π Roadmap
π€ Contributing
Contributions are welcome!
- Fork the repo
- Create your feature branch (
git checkout -b feature/my-feature
) - Commit your changes (
git commit -m 'Add some feature'
) - Push to the branch (
git push origin feature/my-feature
) - Create a Pull Request
π License
This project is licensed under the MIT License. See the LICENSE file for details.
π¨βπ» Author
Neethu KT
- GitLab: connect_secure
π This will also help increase your pub.flutter-io.cn score, since your README will clearly explain the package.
Do you want me to also draft a CHANGELOG.md (with v1.0.0 - Initial release with SSL Pinning support
), so your package looks more polished?