Reuse existing library for generating secure random string
This commit is contained in:
parent
b4940d4fdd
commit
797c6ae856
|
@ -1,3 +1,4 @@
|
||||||
|
import 'package:encrypt/encrypt.dart';
|
||||||
import 'package:photos/utils/crypto_util.dart';
|
import 'package:photos/utils/crypto_util.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
@ -75,7 +76,7 @@ class Configuration {
|
||||||
}
|
}
|
||||||
|
|
||||||
void generateAndSaveKey(String passphrase) async {
|
void generateAndSaveKey(String passphrase) async {
|
||||||
final key = CryptoUtil.createCryptoRandomString();
|
final key = SecureRandom(32).base64;
|
||||||
await _preferences.setString(keyKey, CryptoUtil.encrypt(key, passphrase));
|
await _preferences.setString(keyKey, CryptoUtil.encrypt(key, passphrase));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,6 @@
|
||||||
import 'dart:convert';
|
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import 'package:encrypt/encrypt.dart';
|
import 'package:encrypt/encrypt.dart';
|
||||||
|
|
||||||
class CryptoUtil {
|
class CryptoUtil {
|
||||||
static final Random _random = Random.secure();
|
|
||||||
|
|
||||||
static String createCryptoRandomString([int length = 32]) {
|
|
||||||
var values = List<int>.generate(length, (i) => _random.nextInt(256));
|
|
||||||
return base64Url.encode(values);
|
|
||||||
}
|
|
||||||
|
|
||||||
static String encrypt(String plainText, String key) {
|
static String encrypt(String plainText, String key) {
|
||||||
final encrypter = Encrypter(AES(Key.fromUtf8(key)));
|
final encrypter = Encrypter(AES(Key.fromUtf8(key)));
|
||||||
return encrypter.encrypt(plainText).base64;
|
return encrypter.encrypt(plainText).base64;
|
||||||
|
|
Loading…
Reference in a new issue