/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.security;

import java.util.Random;

public class PasswordGenerator
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String[] syllabary = {
    "ba", "be", "bi", "bo", "bu",
    "da", "de", "di", "do", "du",
    "cha", "chi", "cho", "chu",
    "fa", "fe", "fi", "fo", "fu",
    "ga", "ge", "gi", "go", "gu",
    "ha", "he", "hi", "ho", "hu",
    "ja", "je", "ji", "jo", "ju",
    "ka", "ke", "ki", "ko", "ku",
    "la", "le", "li", "lo", "lu",
    "ma", "me", "mi", "mo", "mu",
    "na", "ne", "ni", "no", "nu",
    "pa", "pe", "pi", "po", "pu",
    "ra", "re", "ri", "ro", "ru",
    "sa", "se", "si", "so", "su",
    "sha", "she", "sho", "shu",
    "ta", "te", "ti", "to", "tu",
    "va", "ve", "vi", "vo", "vu",
    "wa", "we", "wi", "wo", "wu",
    "ya", "ye", "yi", "yo", "yu",
    "za", "ze", "zi", "zo", "zu"
  };  // 98 syllables

  private static final char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String value;      // the password value

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public PasswordGenerator()
  {
    Random rng = new Random();
    StringBuffer buf = new StringBuffer();
    int i;
    for (i=0; i<4; i++)   // 4 random syllables
      buf.append(syllabary[rng.nextInt(syllabary.length)]);
    for (i=0; i<3; i++)   // 3 random digits
      buf.append(digits[rng.nextInt(digits.length)]);
    value = buf.toString();
    // total possible number of passwords = 92,236,816,000

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  public String toString()
  {
    return value;

  } // end toString

  public boolean equals(Object obj)
  {
    if (obj==null)
      return false;  // trivial case
    else if (obj==(Object)this)
      return true;   // trivial case
    if (obj instanceof PasswordGenerator)
    { // compare value of PasswordHash argument to value
      PasswordGenerator other = (PasswordGenerator)obj;
      return value.equals(other.value);

    } // end if
    else
      return value.equals(obj.toString());

  } // end equals

  public int hashCode()
  {
    return value.hashCode();

  } // end hashCode

} // end class PasswordGenerator
