Generate key pair and signing (NIST P-256 EC key pair using ECDSA) using Local Authentication for Android and iOS.
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"
final _localAuthSignature = LocalAuthSignature.instance;
final _key = 'com.prongbang.signx.key';
- 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
}
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}');
}
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}');
}
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}');
}
- 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" }
}
}
- Add privacy in
info.plist
file
<dict>
<key>NSFaceIDUsageDescription</key>
<string>This application wants to access your TouchID or FaceID</string>
</dict>