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

  • Support multiple SSL pinning modes (certificate, public key)
  • Add support for other HTTP clients (http package)
  • Add automatic fingerprint generation tool

🀝 Contributing

Contributions are welcome!

  1. Fork the repo
  2. Create your feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'Add some feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Create a Pull Request

πŸ“œ License

This project is licensed under the MIT License. See the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

Neethu KT


πŸ‘‰ 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?

Libraries

connect_secure