/*
 * 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();

    /**
     * Generate a hex string of the byte content with an 'a' character in the front of
     * the string.
     *
     * @param b
     * @return
     */
    private static String generateRequestIdString(byte[] b)
    {
        char[] out = new char[(b.length * 2)+1];
        out[0] = 'a';
        for (int i = 0; i < b.length; i++)
        {
            out[i*2 + 1] = hexes[(b[i] >> 4) & 0xf];
            out[i*2 + 2] = 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 generateRequestIdString(bytes);
    }
}

@@ -803,213 +803,39 @@
     
-/**%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    private static String generateRequestIdString
+private static String hexEncode
 (byt
@@ -877,17 +877,16 @@
 ew char%5B
-(
 b.length
@@ -893,36 +893,11 @@
  * 2
-)+1
 %5D;%0A
-        out%5B0%5D = 'a';%0A
     
@@ -968,12 +968,8 @@
 %5Bi*2
- + 1
 %5D = 
@@ -1016,17 +1016,17 @@
 t%5Bi*2 + 
-2
+1
 %5D = hexe
@@ -1444,31 +1444,23 @@
 urn 
-generateRequestIdString
+%22_%22 + hexEncode
 (byt

simplify generateRequestId code a bit