Skip to content

Conversation

@bvdbasch
Copy link

This PR contains the first version of a cryptographically secure password generator.

Note

The goal here was not to write something novel or new; I am well aware that there are already several password generators available that are built with Rust. This is purely an exersize for me to gain more proficiency in Rust, and to update an old tool from my toolset to something modern.

For those reviewing this PR I am particularly interested in feedback on the following

  • Did I follow conventions?
  • Is the approach I took - separation of concerns by using struct and creating an implementation for it - a good one, or is it absolutely the wrong way to go about this in simple scripts?
  • Is there a better way (efficiency/performance) to achieve the same result?

For additional context, here is the PHP code that this Rust crate replaced:

<?php

$pass = '';
$alphanum = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

# Format is always 4-...
for ($i = 0; $i <= 23; $i++) {
    switch($i){
        case 4:
        case 9:
        case 14:
        case 19:
            $pass .= '-';
            break;
        default:
            $pass .= $alphanum[random_int(0, strlen($alphanum) - 1)];
    }
}

echo $pass . PHP_EOL;

For those not proficient with php here is what the code above does:

  1. run passgen.php from your terminal
  2. It executes a small loop where it picks one of the alphanumerical characters at random (using a cryptographically secure random number generator) at a time.
  3. A block is 4 chars, so at position 4,9,14,19 it inserts a hyphen (-) instead.
  4. After the loop finishes the generated password is printed in the terminal followed by a new line. A password will look like this: fLH7-KpZg-HMWx-2CEV-G8L.

Thank you for reviewing this code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant