public static String generateNonce ( int length ) { if ( length < 0 ) { if ( tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "Negative length provided. Will default to nonce of length " + NONCE_LENGTH ) ; } length = NONCE_LENGTH ; } StringBuilder randomString = new StringBuilder ( ) ; SecureRandom r = new SecureRandom ( ) ; for ( int i = 0 ; i < length ; i ++ ) { int index = r . nextInt ( chars . length ) ; randomString . append ( chars [ index ] ) ; } return randomString . toString ( ) ; }
Generates a random string with the specified length .