diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 7492771..cae4e5b 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -270,8 +270,14 @@ public class Agent if (StackProperties.getString(StackProperties.SOFTWARE) == null) System.setProperty(StackProperties.SOFTWARE, "ice4j.org"); - ufrag = new BigInteger(24, random).toString(32); - password = new BigInteger(128, random).toString(32); + ufrag + = ensureIceAttributeLength( + new BigInteger(24, random).toString(32), + /* min */ 4, /* max */ 256); + password + = ensureIceAttributeLength( + new BigInteger(128, random).toString(32), + /* min */ 22, /* max */ 256); tieBreaker = Math.abs(random.nextLong()); nominator = new DefaultNominator(this); @@ -1966,6 +1972,57 @@ public class Agent setState(terminationState); } + /** + * Adds or removes ICE characters (i.e. ALPHA, DIGIT, +, or /) to or from a + * specific String in order to produce a String with a + * length within a specific range. + * + * @param s the String to add or remove characters to or from in + * case its length is less than min or greater than max + * @param min the minimum length in (ICE) characters of the returned + * String + * @param max the maximum length in (ICE) characters of the returned + * String + * @return s if its length is greater than or equal to + * min and less than or equal to max; a new + * String which is equal to s with prepended ICE + * characters if the length of s is less than min; a new + * String which is composed of the first max characters of + * s if the length of s is greater than max + * @throws IllegalArgumentException if min is negative or + * max is less than min + * @throws NullPointerException if s is equal to null + */ + private String ensureIceAttributeLength(String s, int min, int max) + { + if (s == null) + throw new NullPointerException("s"); + if (min < 0) + throw new IllegalArgumentException("min " + min); + if (max < min) + throw new IllegalArgumentException("max " + max); + + int length = s.length(); + int numberOfIceCharsToAdd = min - length; + + if (numberOfIceCharsToAdd > 0) + { + StringBuilder sb = new StringBuilder(min); + + for (; numberOfIceCharsToAdd > 0; --numberOfIceCharsToAdd) + { + sb.append('0'); + } + sb.append(s); + s = sb.toString(); + } + else if (max < length) + { + s = s.substring(0, max); + } + return s; + } + /** * Called by the garbage collector when garbage collection determines that * there are no more references to this instance. Calls {@link #free()} on