currently I'm trying to send credential Data to my Backend, therefore I want to hash the password for security purposes.
But when I use the method PassCrypt().hashPass("", passwd, 48)
, the complete App freezes for nearly 1-2 seconds. Is there a way to await the input asynchronously?
Thanks :)
You can copy paste run full code below
You can use compute
and get result with then
In demo, you can see Floating action button is not blocked
code snippet
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);
});
working demo
output
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
full code
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),
),
);
}
}