/*
 * SAMLUtils - Utility functions
 *
 * 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.
 *
 * Copyright (c) 2014 LastPass, Inc.
 */
package com.lastpass.saml;

import java.security.SecureRandom;

public class SAMLUtils
{
    private static final char[] hexes = "0123456789abcdef".toCharArray();
    private static String hexEncode(byte[] b)
    {
        char[] out = new char[b.length * 2];
        for (int i = 0; i < b.length; i++)
        {
            out[i*2] = hexes[(b[i] >> 4) & 0xf];
            out[i*2 + 1] = hexes[b[i] & 0xf];
        }
        return new String(out);
    }

    /**
     *  Generate a request ID suitable for passing to
     *  SAMLClient.createAuthnRequest.
     */
    public static String generateRequestId()
    {
        /* compute a random 256-bit string and hex-encode it */
        SecureRandom sr = new SecureRandom();
        byte[] bytes = new byte[32];
        sr.nextBytes(bytes);
        return hexEncode(bytes);
    }
}

@@ -793,16 +793,177 @@
 Array();
+%0A%0A    /**%0A     * Generate a hex string of the byte content with an 'a' character in the front of%0A     * the string.%0A     *%0A     * @param b%0A     * @return%0A     */
 %0A    pri
@@ -981,25 +981,39 @@
  String 
-hexEncode
+generateRequestIdString
 (byte%5B%5D 
@@ -1051,16 +1051,17 @@
 ew char%5B
+(
 b.length
@@ -1064,19 +1064,44 @@
 ngth * 2
+)+1
 %5D;%0A
+        out%5B0%5D = 'a';%0A
         
@@ -1164,16 +1164,20 @@
  out%5Bi*2
+ + 1
 %5D = hexe
@@ -1212,33 +1212,33 @@
       out%5Bi*2 + 
-1
+2
 %5D = hexes%5Bb%5Bi%5D &
@@ -1648,17 +1648,31 @@
 urn 
-hexEncode
+generateRequestIdString
 (byt

Generate a request id that always start with a a character.