package org.springframework.security.oauth2.common.util;

import java.security.SecureRandom;
import java.util.Random;

/**
 * Utility that generates a random-value ASCII string.
 * 
 * @author Ryan Heaton
 * @author Dave Syer
 */
public class RandomValueStringGenerator {

	private static final char[] DEFAULT_CODEC = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
			.toCharArray();

	private Random random = new SecureRandom();
	private int length = 6;

	public String generate() {
		byte[] verifierBytes = new byte[length];
		random.nextBytes(verifierBytes);
		return getAuthorizationCodeString(verifierBytes);
	}

	/**
	 * Convert these random bytes to a verifier string. The length of the byte array can be
	 * {@link #setLength(int) configured}. Default implementation mods the bytes to fit into the ASCII letters
	 * 1-9, A-Z, a-z .
	 * 
	 * @param verifierBytes The bytes.
	 * @return The string.
	 */
	protected String getAuthorizationCodeString(byte[] verifierBytes) {
		char[] chars = new char[verifierBytes.length];
		for (int i = 0; i < verifierBytes.length; i++) {
			chars[i] = DEFAULT_CODEC[((verifierBytes[i] & 0xFF) % DEFAULT_CODEC.length)];
		}
		return new String(chars);
	}

	/**
	 * The random value generator used to create token secrets.
	 * 
	 * @param random The random value generator used to create token secrets.
	 */
	public void setRandom(Random random) {
		this.random = random;
	}

	/**
	 * The verifier length in bytes, before being encoded to a string.
	 * 
	 * @param length The verifier length in bytes, before being encoded to a string.
	 */
	public void setLength(int length) {
		this.length = length;
	}

}

@@ -441,16 +441,17 @@
 ndom();%0A
+%0A
 %09private
@@ -465,13 +465,335 @@
 ngth
- = 6;
+;%0A%0A%09/**%0A%09 * Create a generator with the default length (6).%0A%09 */%0A%09public RandomValueStringGenerator() %7B%0A%09%09this(6);%0A%09%7D%0A%0A%09/**%0A%09 * Create a generator of random strings of the length provided%0A%09 * %0A%09 * @param length the length of the strings generated%0A%09 */%0A%09public RandomValueStringGenerator(int length) %7B%0A%09%09this.length = length;%0A%09%7D
 %0A%0A%09p
@@ -1086,17 +1086,21 @@
 gured%7D. 
-D
+The d
 efault i
@@ -1143,16 +1143,20 @@
 into the
+%0A%09 *
  ASCII l
@@ -1161,20 +1161,16 @@
  letters
-%0A%09 *
  1-9, A-
@@ -1748,24 +1748,25 @@
  random;%0A%09%7D%0A
+%09
 %0A%09/**%0A%09 * Th
@@ -1771,66 +1771,36 @@
 The 
-verifier length in bytes, before being encoded to a string
+length of string to generate
 .%0A%09 
@@ -1824,71 +1824,25 @@
 gth 
-The verifier length in bytes, before being encoded to a string.
+the length to set
 %0A%09 *

Add utility constructor to RandomValueStringGenerator