password snippets

Generating a password in Java

Tagged password, generator, java  Languages java

A simple password generator class.

 * Simple password generator.
 * @author marko haapala at aktagon com
public class PasswordGenerator {
  public static final char[] HEX_CHARS = { 'a','b','c','d','e','f','g','h','0','1','2','3','4','5','6','7','8','9' };
  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',
                                              '!','"','#','¤','%','&','/','(',')' };
   * Generates an eight characters long password consisting of hexadecimal characters.
   * @return the generated password
  public static String generate() {
    return generate(HEX_CHARS, 8);

   * Generates a password consisting of hexadecimal characters.
   * @param length of the password
   * @return the generated password
  public static String generate(final int length) {
    return generate(HEX_CHARS, length);

   * Generates a password according to the given parameters.
   * @param characters that make up the password
   * @param length of the password
   * @return the generated password
  public static String generate(final char[] characters, final int length) {
    RandomString randomString = new RandomString(PseudoRandom.getRandom(), characters);
    return randomString.getString(length);

How to reset the MySQL root password

Tagged mysql, password, reset  Languages bash

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:

/etc/init.d/mysql stop

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

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

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

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

Remember to delete the file:

rm /tmp/mysql_init.txt

How to generate a pronouncable password with PHP

Tagged password, generate, random, php  Languages php

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

class Password
    static function generate($length)

        $vowels = array("a", "e", "i", "o", "u");
        $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
        "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl"); 

        $num_vowels = count($vowels);
        $num_cons = count($cons);

        $password = '';
        for($i = 0; $i < $length; $i++)
            $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];

        return substr($password, 0, $length);

To use it try this code:

require 'password.php'

echo Password.generate(10);

It will spit out passwords that are semi-pronouncable.

A random password generator for Ruby

Tagged random, password, ruby  Languages ruby

This is a simple random password generator for Ruby that allows you to customize the characters included in the password:

CHARS = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
def random_password(length=10)
  CHARS.sort_by { rand }.join[0...length]

In this example, the generated passwords contain numbers, uppercase, and lowercase characters.

How to prevent Capistrano from asking for password when deploying

Tagged capistrano, ssh, password  Languages ruby

Capistrano might ask for a password when:

# Capistrano is using sudo, so set it to false
set :use_sudo, false

# Your remote server is trying to checkout an SSH protected Git repository
set :repository, '/var/git/repositories/xxx.git' # Remote server also holds the git repository
set :local_repository, 'ssh://xxx/var/git/repositories/xxx.git' # Your development machine points to the remote machine 

# SSH settings, also see ~/.ssh/config
set :user, "jebus"
set :domain, ''
set :port, 666

# Other settings worth checking
ssh_options[:forward_agent] = true
ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "id_rsa")]
default_run_options[:pty] = true # see[email protected]/msg07323.html for details


How to setup a password-less “cap deploy” with Capistrano

Updating Passwords and Emails When Using Active Admin and Devise

Tagged active_admin, password, email, devise  Languages ruby

With Active Admin and Devise, we want to: * Skip email confirmations * Require password and email confirmations, but only when changing the email or password

This code supports our requirements:

ActiveAdmin.register User do
  controller do
    def update
      if params[:user][:password].blank?
      if params[:user][:email].blank?

  before_save do |user|

See the Active Admin DSL for details

How to encrypt/decrypt a file with GPG

Tagged decrypt, encrypt, gpg, password  Languages bash

First, install GPG:

brew install gpg

To encrypt a file with GPG, run:

gpg -c secrets.txt

To decrypt the encrypted and password-protected GPG file, run:

gpg secrets.txt.gpg