Created
February 21, 2020 22:45
-
-
Save zicklag/6e89fddfa99b8d07a861f17df3719154 to your computer and use it in GitHub Desktop.
Password generation in Rust
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Rust Password Generator" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"// First we create our corpus, or set of characters that will go in our password\n", | |
"const PASSWORD_CHARS: &str = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\";\n", | |
"\n", | |
"// Import the random generator and the interator random extension trait\n", | |
":dep rand\n", | |
"extern crate rand;\n", | |
"use rand::{seq::IteratorRandom, thread_rng, Rng};\n", | |
"\n", | |
"// Then we create our password generator\n", | |
"fn gen_passwd(len: u16, rng: &mut impl Rng) -> String {\n", | |
" // The password buffer\n", | |
" let mut password = String::new();\n", | |
" \n", | |
" // Loop trough 0 up to our password length\n", | |
" for _ in 0..len {\n", | |
" // Add a character\n", | |
" password.push(\n", | |
" PASSWORD_CHARS // Out of our password character list\n", | |
" .chars() // As an iterator over the characters\n", | |
" .choose(rng) // This choose method comes form the `rand::seq::IteratorRandom` import\n", | |
" .expect(\"Empty iterator for password chars\"), // Expect won't fail unless our corpus was empty\n", | |
" );\n", | |
" }\n", | |
" \n", | |
" password\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"// To use our function we have to first create a random generator\n", | |
"let mut rng = thread_rng();" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"owrYciSdFriwSDn9O0BouigTD1fORmWG\"" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"// Then we can generate random numbers!\n", | |
"gen_passwd(32, &mut rng)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"s04qoXRqHOoL\"" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"gen_passwd(12, &mut rng)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"KIazGvnqcUgxCYF91rbuGPapoN4uhFykI2E9IKl4wi6QFNlOFCV8CsaKdCyi7T0i2KmiXX0EfkjUPMXpGXBRgzCoPVPmxKAXeUHfOhOrHspDNNBcalQy6KYljr80\"" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"gen_passwd(124, &mut rng)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Rust", | |
"language": "rust", | |
"name": "rust" | |
}, | |
"language_info": { | |
"codemirror_mode": "rust", | |
"file_extension": ".rs", | |
"mimetype": "text/rust", | |
"name": "Rust", | |
"pygment_lexer": "rust", | |
"version": "" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment