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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
import 'package:flutter/material.dart';
import 'package:crypto/crypto.dart';
import 'dart:convert';
enum CryptoType {
sha1,
sha224,
sha256,
sha384,
sha512,
sha512_224,
sha512_256,
md5,
hmac224,
hmac256,
hmac384,
hmac512,
hmac512_224,
hmac512_256,
hmacmd5,
}
class CryptoPage extends StatefulWidget {
final String title;
const CryptoPage({super.key, required this.title});
@override
State<CryptoPage> createState() => _CryptoPageState();
}
class _CryptoPageState extends State<CryptoPage> {
static final hmacKey = utf8.encode('test_p@assw0rd1_hamc');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Center(
child: Column(
children: _buildButtons(),
),
),
));
}
List<Widget> _buildButtons() {
List<CryptoType> types = CryptoType.values;
return types.map((type) {
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 10,
),
child: ElevatedButton(
onPressed: () => _hashString('hello world', type),
child: Text(_getButtonLabel(type)),
),
);
}).toList();
}
String _getButtonLabel(CryptoType type) {
return type.toString().split('.').last; // 获取枚举项的字符串形式
}
String _hashString(String text, CryptoType type) {
if (text.isEmpty) {
return '';
}
var bytes = utf8.encode(text); // data being hashed
var convert;
switch (type) {
case CryptoType.sha1:
convert = sha1;
break;
case CryptoType.sha224:
convert = sha224;
break;
case CryptoType.sha256:
convert = sha256;
break;
case CryptoType.sha384:
convert = sha384;
break;
case CryptoType.sha512:
convert = sha512;
break;
case CryptoType.sha512_224:
convert = sha512224;
break;
case CryptoType.sha512_256:
convert = sha512256;
break;
case CryptoType.md5:
convert = md5;
break;
case CryptoType.hmac224:
convert = Hmac(sha224, hmacKey);
break;
case CryptoType.hmac256:
convert = Hmac(sha256, hmacKey);
break;
case CryptoType.hmac384:
convert = Hmac(sha384, hmacKey);
break;
case CryptoType.hmac512:
convert = Hmac(sha512, hmacKey);
break;
case CryptoType.hmac512_224:
convert = Hmac(sha512224, hmacKey);
break;
case CryptoType.hmac512_256:
convert = Hmac(sha512256, hmacKey);
break;
case CryptoType.hmacmd5:
convert = Hmac(md5, hmacKey);
break;
default:
}
var res = convert != null ? convert.convert(bytes).toString() : '';
debugPrint('_hashString:$res');
return res;
}
}
|