Moved hash function into seperate file.

NOTE: useful for changePassword,
which requires the hash password functionality as well.
This commit is contained in:
Jakob Meier 2023-03-25 14:27:47 +01:00
parent e3ca4fafa6
commit 569dda01fd
No known key found for this signature in database
GPG key ID: 66BDC7E6A01A6152
2 changed files with 118 additions and 121 deletions

8
lib/backend/crypto.dart Normal file
View file

@ -0,0 +1,8 @@
import 'dart:convert';
import 'package:crypto/crypto.dart';
String hashPassword(String pw) {
var bytes = utf8.encode(pw);
final password = sha256.convert(bytes).toString();
return password;
}

View file

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:outbag_app/backend/crypto.dart';
import 'package:outbag_app/backend/request.dart'; import 'package:outbag_app/backend/request.dart';
import 'package:outbag_app/backend/user.dart'; import 'package:outbag_app/backend/user.dart';
import 'package:outbag_app/tools/fetch_wrapper.dart'; import 'package:outbag_app/tools/fetch_wrapper.dart';
@ -128,8 +129,7 @@ class _AuthPageState extends State<AuthPage> {
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
child: TextField( child: TextField(
controller: _ctrPasswordRpt, controller: _ctrPasswordRpt,
keyboardType: keyboardType: TextInputType.visiblePassword,
TextInputType.visiblePassword,
obscureText: true, obscureText: true,
decoration: const InputDecoration( decoration: const InputDecoration(
prefixIcon: Icon(Icons.lock), prefixIcon: Icon(Icons.lock),
@ -150,13 +150,11 @@ class _AuthPageState extends State<AuthPage> {
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
child: TextField( child: TextField(
controller: _ctrOTA, controller: _ctrOTA,
keyboardType: keyboardType: TextInputType.visiblePassword,
TextInputType.visiblePassword,
decoration: const InputDecoration( decoration: const InputDecoration(
prefixIcon: Icon(Icons.key), prefixIcon: Icon(Icons.key),
labelText: 'OTA', labelText: 'OTA',
hintText: hintText: 'One-Time-Authorization token',
'One-Time-Authorization token',
helperText: helperText:
'This token might be required if the server is rate limited', 'This token might be required if the server is rate limited',
border: OutlineInputBorder(), border: OutlineInputBorder(),
@ -166,9 +164,7 @@ class _AuthPageState extends State<AuthPage> {
] ]
: []), : []),
], ],
) ))),
)
),
floatingActionButton: FloatingActionButton.extended( floatingActionButton: FloatingActionButton.extended(
onPressed: () async { onPressed: () async {
setState(() { setState(() {
@ -227,13 +223,9 @@ class _AuthPageState extends State<AuthPage> {
} }
// hash password // hash password
var bytes = utf8.encode(_ctrPassword.text); final password = hashPassword(_ctrPassword.text);
final password = sha256.convert(bytes).toString();
doNetworkRequest( doNetworkRequest(scaffMgr, needUser: false, req: (_) {
scaffMgr,
needUser: false,
req: (_) {
if (widget.mode == Mode.signin) { if (widget.mode == Mode.signin) {
return postUnauthorized( return postUnauthorized(
target: server, target: server,
@ -264,21 +256,18 @@ class _AuthPageState extends State<AuthPage> {
'OTA': _ctrOTA.text 'OTA': _ctrOTA.text
}); });
} }
}, }, onOK: (body) async {
onOK: (body) async {
// authorize user // authorize user
await User( await User(
username: _ctrUsername.text, username: _ctrUsername.text,
password: password, password: password,
server: server) server: server)
.toDisk(); .toDisk();
}, }, after: () {
after: () {
setState(() { setState(() {
showSpinner = false; showSpinner = false;
}); });
} });
);
}, },
label: Text(modeName), label: Text(modeName),
icon: const Icon(Icons.check), icon: const Icon(Icons.check),