Skip to content

Instantly share code, notes, and snippets.

@gugadev
Created January 25, 2022 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gugadev/30b148bfc59780a29a0b2547a00c356a to your computer and use it in GitHub Desktop.
Save gugadev/30b148bfc59780a29a0b2547a00c356a to your computer and use it in GitHub Desktop.
Generate random alphanumeric password
import 'dart:math';
void main() async {
var pwd = await getRandomPwd(8);
print(pwd);
}
Future<String> getRandomPwd(int length) async {
if (length > 62) {
throw Error('The max length of the password could be 62');
}
const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random _rnd = Random();
return String.fromCharCodes(
Iterable.generate(
length,
(_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment