Password generation is something we all think about doing at some point, but how can we go about doing it - and making these passwords easy to remember and secure? In this tutorial, I will walk you through how I recently built a password generator for fun and what my thoughts were.
interface GeneratorContract { /** * @return string */ public function generate(): string; /** * @return string */ public function generateSecure(): string; }
trait HasWords { /** * @param array $words */ public function __construct( private readonly array $words, ) { } public function random(): string { return $this->words[array_rand($this->words)]; }}
trait HasWords { /** * @param array $words */ public function __construct( private readonly array $words, ) { } public function random(): string { return $this->words[array_rand($this->words)]; } public function secure(): string { $word = $this->random(); $asArray = str_split($word); $secureArray = array_map( callback: fn (string $item): string => $this->convertToNumerical($item), array: $asArray, ); return implode('', $secureArray); } public function convertToNumerical(string $item): string { return match ($item) { 'a' => '4', 'e' => '3', 'i' => '1', 'o' => '0', default => $item, }; }}