Dart:PointyCastle 异步计算
Dart: PointyCastle compute asynchronous
目前我正在尝试将凭据数据发送到我的后端,因此出于安全目的我想散列密码。
但是当我使用PassCrypt().hashPass("", passwd, 48)
的方法时,整个App卡顿了将近1-2秒。有没有办法异步等待输入?
谢谢:)
您可以复制粘贴运行下面的完整代码
您可以使用 compute
并使用 then
获得结果
在演示中,您可以看到浮动操作按钮未被阻止
代码片段
Future<String> hashJob(String password) async {
return compute(hashPassword, password);
}
String hashPassword(String password) {
return PassCrypt().hashPass("", password, 48);
}
hashJob("password test").then((value) {
hashedPassword = value;
print(hashedPassword);
});
工作演示
输出
I/flutter ( 9820): 2020-02-20 09:06:53.904373
I/flutter ( 9820): 2020-02-20 09:06:54.559547
I/flutter ( 9820): 2020-02-20 09:06:54.560054
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w
完整代码
import 'package:flutter/material.dart';
import 'package:steel_crypt/steel_crypt.dart';
import 'dart:async';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
Future<String> hashJob(String password) async {
return compute(hashPassword, password);
}
String hashPassword(String password) {
return PassCrypt().hashPass("", password, 48);
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String hashedPassword;
void _incrementCounter() {
/*print(DateTime.now());
var passHash = PassCrypt().hashPass("", "password test", 48);
print(DateTime.now());
print(passHash);*/
print(DateTime.now());
hashJob("password test").then((value) {
hashedPassword = value;
print(hashedPassword);
});
print(DateTime.now());
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
目前我正在尝试将凭据数据发送到我的后端,因此出于安全目的我想散列密码。
但是当我使用PassCrypt().hashPass("", passwd, 48)
的方法时,整个App卡顿了将近1-2秒。有没有办法异步等待输入?
谢谢:)
您可以复制粘贴运行下面的完整代码
您可以使用 compute
并使用 then
获得结果
在演示中,您可以看到浮动操作按钮未被阻止
代码片段
Future<String> hashJob(String password) async {
return compute(hashPassword, password);
}
String hashPassword(String password) {
return PassCrypt().hashPass("", password, 48);
}
hashJob("password test").then((value) {
hashedPassword = value;
print(hashedPassword);
});
工作演示
输出
I/flutter ( 9820): 2020-02-20 09:06:53.904373
I/flutter ( 9820): 2020-02-20 09:06:54.559547
I/flutter ( 9820): 2020-02-20 09:06:54.560054
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w
完整代码
import 'package:flutter/material.dart';
import 'package:steel_crypt/steel_crypt.dart';
import 'dart:async';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
Future<String> hashJob(String password) async {
return compute(hashPassword, password);
}
String hashPassword(String password) {
return PassCrypt().hashPass("", password, 48);
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String hashedPassword;
void _incrementCounter() {
/*print(DateTime.now());
var passHash = PassCrypt().hashPass("", "password test", 48);
print(DateTime.now());
print(passHash);*/
print(DateTime.now());
hashJob("password test").then((value) {
hashedPassword = value;
print(hashedPassword);
});
print(DateTime.now());
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}