package cn.citycraft.AuthMe.security;

import java.util.Calendar;
import java.util.Random;

/**
 *
 * @author Xephi59
 */
public class RandomString {

	private static final char[] chars = new char[36];

	private final char[] buf;

	private final Random random = new Random();

	public RandomString(int length) {
		if (length < 1)
			throw new IllegalArgumentException("length < 1: " + length);
		buf = new char[length];
		random.setSeed(Calendar.getInstance().getTimeInMillis());
	}

	static {
		for (int idx = 0; idx < 10; ++idx)
			chars[idx] = (char) ('0' + idx);
		for (int idx = 10; idx < 36; ++idx)
			chars[idx] = (char) ('a' + idx - 10);
	}

	public String nextString() {
		for (int idx = 0; idx < buf.length; ++idx)
			buf[idx] = chars[random.nextInt(chars.length)];
		return new String(buf);
	}

}
