Reuse existing library for generating secure random string

This commit is contained in:
Vishnu Mohandas 2020-08-10 05:49:44 +05:30
parent b4940d4fdd
commit 797c6ae856
2 changed files with 2 additions and 11 deletions

View file

@ -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));
} }

View file

@ -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;