Skip to content

Generate key pair and signing (NIST P-256 EC key pair using ECDSA) using Local Authentication for Android and iOS.

License

Notifications You must be signed in to change notification settings

prongbang/local_auth_signature

Repository files navigation

local_auth_signature

pub package

Generate key pair and signing (NIST P-256 EC key pair using ECDSA) using Local Authentication for Android and iOS.

Screenshot

Getting started

It is really easy to use! You should ensure that you add the local_auth_signature as a dependency in your flutter project.

local_auth_signature: "^1.0.12"

Usage

Flutter

New instance

final _localAuthSignature = LocalAuthSignature.instance;

Define Key

final _key = 'com.prongbang.signx.key';

Check Fingerprint or Biometric changed

  • Android, iOS
final status = await _localAuthSignature.isBiometricChanged(_key);
  • iOS
await _localAuthSignature.resetBiometricChanged();
try {
    final signature = await _localAuthSignature.sign(_key, androidPrompt, iosPrompt);
} on PlatformException catch (e) {
  // TODO check from exception code
}

Create KeyPair

try {
    final publicKey = await _localAuthSignature.createKeyPair(
        _key,
        AndroidPromptInfo(
            title: 'BIOMETRIC',
            subtitle: 'Please allow biometric',
            negativeButton: 'CANCEL',
        ),
        IOSPromptInfo(reason: 'Please allow biometric'),
    );
    print('publicKey: $publicKey');
} on PlatformException catch (e) {
  print('PlatformException: ${e.code}');
}

Sign

final _payload = 'Hello';
try {
    final signature = await _localAuthSignature.sign(
        _key,
        _payload,
        AndroidPromptInfo(
            title: 'BIOMETRIC',
            subtitle: 'Please allow biometric',
            negativeButton: 'CANCEL',
        ),
        IOSPromptInfo(reason: 'Please allow biometric'),
    );
    print('signature: $signature');
} on PlatformException catch (e) {
  print('PlatformException: ${e.code}');
}

Verify

try {
    final verified = await _localAuthSignature.verify(
        _key,
        _payload,
        _signature!,
        AndroidPromptInfo(
            title: 'BIOMETRIC',
            subtitle: 'Please allow biometric',
            negativeButton: 'CANCEL',
        ),
        IOSPromptInfo(reason: 'Please allow biometric'),
    );
    print('verified: $verified');
} on PlatformException catch (e) {
    print('PlatformException: ${e.code}');
}

Android

  • Update code in MainActivity.kt file
import io.flutter.embedding.android.FlutterFragmentActivity

class MainActivity : FlutterFragmentActivity()
  • Add use-permissions in AndroidManifest.xml file
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
  • Add jitpack maven repositories in build.gradle
buildscript {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

iOS

  • Add privacy in info.plist file
<dict>
  <key>NSFaceIDUsageDescription</key>
  <string>This application wants to access your TouchID or FaceID</string>
</dict>