/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.util;

import org.bodington.util.*;
import java.util.Properties;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Random;

public class RandomPasswordGenerator implements org.bodington.util.PasswordGenerator
	{
	private static final int length=6;
	
	private char[] allowed_chars;
	Random generator;
	
	
	
	public RandomPasswordGenerator()
		{
		allowed_chars = new char[19];
		"bcdfghjkmnpqrstvwxz".getChars( 0, 19, allowed_chars, 0 );
		generator = new Random( System.currentTimeMillis() );
		}
	
	public void setAllowedCharacters( String str )
		throws PasswordGeneratorException
		{
		if ( allowed_chars == null )
			throw new PasswordGeneratorException( "Null string of allowed characters is not supported." );
			
		str.getChars( 0, str.length(), allowed_chars, 0 );
		}
	
    public void setGlobalProperties(Properties props) throws PasswordGeneratorException
    	{
    	}
    	
    public Enumeration getGlobalPropertyNames()
    	{
    	Vector l = new Vector();
        return l.elements();
    	}

    public Enumeration getUserPropertyNames()
    	{
    	Vector l = new Vector();
        return l.elements();
    	}

    public String generate(Properties props) throws PasswordGeneratorException
    	{
    	char[] s= new char[length];
    	
    	for ( int i=0; i<length; i++ )
    		s[i] = allowed_chars[(generator.nextInt() & 0xff ) % allowed_chars.length];
    		
    	return new String( s );
    	}

	public boolean isOffLine()
		{
		return false;  // password isn't stored off line - software must output it so it can be given to user.
		}

	public String offLineAdvice()
		{
		return "The generated password is was created at random and you must memorise or make a record of it.";
		}
	}
