Register now and start sharing your code snippets.

How to generate a pronouncable password with PHP

PHP posted 6 months ago by christian

This is a slight modification of a script I found at PHPFAQ :

   1  class Password
   2  {
   3  	static function generate($length)
   4  	{
   5  		srand((double)microtime()*1000000);
   6  
   7  		$vowels = array("a", "e", "i", "o", "u");
   8  		$cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
   9  	    "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl"); 
  10  
  11  		$num_vowels = count($vowels);
  12  		$num_cons = count($cons);
  13  
  14  		$password = '';
  15  		
  16  		for($i = 0; $i < $length; $i++)
  17  		{
  18  			$password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];
  19  		}
  20  
  21  		return substr($password, 0, $length);
  22  	}
  23  }

To use it try this code:

   1  require 'password.php'
   2  
   3  echo Password.generate(10);

It will spit out passwords that are semi-pronouncable.

Tagged password, generate, random, php

How to reset the MySQL root password

Shell Script (Bash) posted 10 months ago by christian

I just happened to lock myself out of MySQL, but luckily I have root access to the server so I can reset it easily by first shutting down MySQL:

   1  /etc/init.d/mysql stop

And then creating a MySQL init file with the desired password:

   1  $ echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('1234');" >> /tmp/mysql_init.txt

Starting MySQL with the —init-file parameter like this resets the password:

   1  $ mysqld_safe --init-file=/tmp/mysql_init.txt

Remember to delete the file:

   1  rm /tmp/mysql_init.txt

Tagged mysql, password, reset

Generating a password in Java

Java posted about 1 year ago by marko

A simple password generator class.

   1  /**
   2   * Simple password generator.
   3   *
   4   * @author marko haapala at aktagon com
   5   */
   6  public class PasswordGenerator {
   7    public static final char[] HEX_CHARS = { 'a','b','c','d','e','f','g','h','0','1','2','3','4','5','6','7','8','9' };
   8    public static final char[] SECURE_CHARS = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
   9                                                'v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  10                                                'Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','=',
  11                                                '!','"','#','¤','%','&','/','(',')' };
  12    /**
  13     * Generates an eight characters long password consisting of hexadecimal characters.
  14     *
  15     * @return the generated password
  16     */
  17    public static String generate() {
  18      return generate(HEX_CHARS, 8);
  19    }
  20  
  21    /**
  22     * Generates a password consisting of hexadecimal characters.
  23     *
  24     * @param length of the password
  25     * @return the generated password
  26     */
  27    public static String generate(final int length) {
  28      return generate(HEX_CHARS, length);
  29    }
  30  
  31    /**
  32     * Generates a password according to the given parameters.
  33     *
  34     * @param characters that make up the password
  35     * @param length of the password
  36     * @return the generated password
  37     */
  38    public static String generate(final char[] characters, final int length) {
  39      RandomString randomString = new RandomString(PseudoRandom.getRandom(), characters);
  40      return randomString.getString(length);
  41    }
  42  }
  43  

Tagged password, generator, java