Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: password generator add show entropy #361

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion authpass/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1751,5 +1751,6 @@
},
"googleDriveMorePermissionsRequired": "Additional permissions needed to access Google Drive.",
"googleDriveRequestPermissionButtonLabel": "Request Permissions",
"scanQrCodeTitle": "Scan QR Code."
"scanQrCodeTitle": "Scan QR Code.",
"entropy": "Entropy:"
}
23 changes: 21 additions & 2 deletions authpass/lib/ui/screens/password_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:authpass/ui/widgets/primary_button.dart';
import 'package:authpass/ui/widgets/slide_hide_widget.dart';
import 'package:authpass/utils/constants.dart';
import 'package:authpass/utils/extension_methods.dart';
import 'package:authpass/utils/password_entropy_calculator.dart';
import 'package:authpass/utils/password_generator.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -94,6 +95,7 @@ class _GeneratePasswordState extends State<GeneratePassword> {
String? _password;
int? _passwordLength = 20;
final _passwordLengthCustom = TextEditingController();
double _entropy = 0;

@override
void initState() {
Expand All @@ -120,6 +122,16 @@ class _GeneratePasswordState extends State<GeneratePassword> {
_password = PasswordGenerator.singleton().generatePassword(
CharacterSetCollection(_selectedCharacterSet.toList()),
_passwordLength!);
_entropy = PasswordEntropyCalculator.calculate(
passwordLength: _passwordLength!,
charSetSize: _selectedCharacterSet
.toList(growable: false)
.whereNotNull()
.fold<int>(
0,
(previousValue, element) => previousValue + element.length,
),
);
});
}

Expand All @@ -140,8 +152,8 @@ class _GeneratePasswordState extends State<GeneratePassword> {
ClipboardData(text: _password ?? CharConstants.empty));
ScaffoldMessenger.of(context)
..hideCurrentSnackBar(reason: SnackBarClosedReason.remove)
..showSnackBar(
SnackBar(content: Text(loc.copiedToClipboard)));
..showSnackBar(SnackBar(
content: Text(loc.copiedToClipboard))); // NON-NLS
},
child: InputDecorator(
decoration: InputDecoration(
Expand All @@ -161,6 +173,13 @@ class _GeneratePasswordState extends State<GeneratePassword> {
),
),
),
if (_entropy != 0)
Padding(
padding: const EdgeInsets.only(left: 16, bottom: 8),
child: Text(
'${loc.entropy} ${_entropy.toStringAsFixed(2)}', // NON-NLS
),
),
SimpleGridWidget(
children: _characterSets.entries
.map<Widget>(
Expand Down
12 changes: 12 additions & 0 deletions authpass/lib/utils/password_entropy_calculator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'dart:math';

class PasswordEntropyCalculator {
// entropy = passLength * log2(charSetSizd)
static double calculate({
required int passwordLength,
required int charSetSize,
}) {
double logBase(num x, num base) => log(x) / log(base);
return passwordLength * logBase(charSetSize, 2);
}
}