/
main.dart
127 lines (115 loc) · 3.83 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_biometrics/flutter_biometrics.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _publicKey = 'Not retrieved/Not set';
String _signature = 'Unknown';
String _payload = 'Zmx1dHRlcl9iaW9tZXRyaWNz';
@override
void initState() {
super.initState();
}
Future<void> createKeys() async {
var biometrics = FlutterBiometrics();
String publicKey = await biometrics.createKeys(
reason: 'Please authenticate to create public/private key pair');
setState(() {
_publicKey = publicKey;
});
if (!mounted) return;
}
Future<void> sign() async {
var biometrics = FlutterBiometrics();
String signature = await biometrics.sign(
payload: _payload,
reason: 'Please authenticate to sign specified payload');
setState(() {
_signature = signature;
});
if (!mounted) return;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('flutter_biometrics'),
backgroundColor: Colors.blueGrey,
),
body: ListView(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 20.0),
alignment: Alignment.center,
child: Text("1. Generate a key pair", style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),),
),
Container(
alignment: Alignment.center,
margin: EdgeInsets.only(top: 20.0),
child: RaisedButton(
onPressed: createKeys,
child: Text("Create keys"),
),
),
Container(
alignment: Alignment.center,
width: double.infinity,
padding: EdgeInsets.all(10.0),
child: Text(
"Public key: $_publicKey",
style: TextStyle(color: Colors.white),
),
margin: EdgeInsets.only(top: 10.0),
color: Colors.blueGrey,
),
Container(
margin: EdgeInsets.only(top: 20.0, bottom: 10.0),
alignment: Alignment.center,
child: Text("2. Provide a payload to sign", style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),),
),
Container(
margin: EdgeInsets.only(top: 20.0),
alignment: Alignment.center,
padding: EdgeInsets.all(10.0),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Payload to sign (Base64 encoded)',
hintText: 'Zmx1dHRlcl9iaW9tZXRyaWNz',
),
onChanged: (value) {
setState(() {
_payload = value;
});
},
),
),
Center(
child: RaisedButton(
onPressed: sign,
child: Text("Sign '$_payload'"),
),
),
Container(
width: double.infinity,
alignment: Alignment.center,
padding: EdgeInsets.all(10.0),
child: Text(
"Signature: $_signature",
style: TextStyle(color: Colors.white),
),
margin: EdgeInsets.only(top: 10.0),
color: Colors.blueGrey,
),
],
),
),
);
}
}