Created
January 25, 2022 14:03
-
-
Save gugadev/30b148bfc59780a29a0b2547a00c356a to your computer and use it in GitHub Desktop.
Generate random alphanumeric password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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