selfprivacy.org.app/lib/utils/password_generator.dart

118 lines
2.7 KiB
Dart
Raw Normal View History

2020-12-03 16:52:53 +00:00
import 'dart:math';
2021-08-18 09:36:40 +00:00
Random _rnd = Random();
typedef StringGeneratorFunction = String Function();
class StringGenerators {
2022-06-05 19:36:32 +00:00
static const String letters = 'abcdefghijklmnopqrstuvwxyz';
static const String numbers = '1234567890';
static const String symbols = '_';
2021-08-18 09:36:40 +00:00
static String getRandomString(
2022-06-05 19:36:32 +00:00
final int length, {
final hasLowercaseLetters = false,
final hasUppercaseLetters = false,
final hasNumbers = false,
final hasSymbols = false,
final isStrict = false,
2021-08-18 09:36:40 +00:00
}) {
2022-06-05 19:36:32 +00:00
String chars = '';
if (hasLowercaseLetters) {
chars += letters;
}
if (hasUppercaseLetters) {
chars += letters.toUpperCase();
}
if (hasNumbers) {
chars += numbers;
}
if (hasSymbols) {
chars += symbols;
}
2021-08-18 09:36:40 +00:00
assert(chars.isNotEmpty, 'chart empty');
if (!isStrict) {
return genString(length, chars);
2020-12-03 16:52:53 +00:00
}
2022-06-05 19:36:32 +00:00
String res = '';
int loose = length;
2021-08-18 09:36:40 +00:00
if (hasLowercaseLetters) {
loose -= 1;
res += genString(1, letters);
2020-12-03 16:52:53 +00:00
}
2021-08-18 09:36:40 +00:00
if (hasUppercaseLetters) {
loose -= 1;
res += genString(1, letters.toUpperCase());
2020-12-03 16:52:53 +00:00
}
2021-08-18 09:36:40 +00:00
if (hasNumbers) {
loose -= 1;
res += genString(1, numbers.toUpperCase());
}
if (hasSymbols) {
loose -= 1;
res += genString(1, symbols);
}
res += genString(loose, chars);
2020-12-03 16:52:53 +00:00
2022-06-05 19:36:32 +00:00
final List<String> shuffledlist = res.split('')..shuffle();
2021-08-18 09:36:40 +00:00
return shuffledlist.join();
2020-12-03 16:52:53 +00:00
}
static String genString(final int length, final String chars) =>
String.fromCharCodes(
Iterable.generate(
length,
(final _) => chars.codeUnitAt(
_rnd.nextInt(chars.length),
),
2021-08-18 09:36:40 +00:00
),
);
2020-12-03 16:52:53 +00:00
2021-08-18 09:36:40 +00:00
static StringGeneratorFunction userPassword = () => getRandomString(
8,
hasLowercaseLetters: true,
hasUppercaseLetters: true,
hasNumbers: true,
isStrict: true,
);
static StringGeneratorFunction passwordSalt = () => getRandomString(
8,
hasLowercaseLetters: true,
);
static StringGeneratorFunction simpleId = () => getRandomString(
5,
hasLowercaseLetters: true,
);
static StringGeneratorFunction dbPassword = () => getRandomString(
40,
hasLowercaseLetters: true,
hasUppercaseLetters: true,
hasNumbers: true,
hasSymbols: true,
);
2022-11-23 07:55:28 +00:00
static StringGeneratorFunction storageName = () => getRandomString(
2021-08-18 09:36:40 +00:00
6,
hasLowercaseLetters: true,
hasUppercaseLetters: false,
2021-08-18 09:36:40 +00:00
hasNumbers: true,
);
2021-12-06 18:31:19 +00:00
2021-11-18 19:10:40 +00:00
static StringGeneratorFunction apiToken = () => getRandomString(
64,
hasLowercaseLetters: true,
hasUppercaseLetters: true,
hasNumbers: true,
);
2020-12-03 16:52:53 +00:00
}