/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

package org.apache.guacamole.auth.jdbc.sharing;

import java.security.SecureRandom;
import javax.xml.bind.DatatypeConverter;

/**
 * An implementation of the ShareKeyGenerator which uses SecureRandom to
 * generate cryptographically-secure random sharing keys.
 * 
 * @author Michael Jumper
 */
public class SecureRandomShareKeyGenerator implements ShareKeyGenerator {

    /**
     * Instance of SecureRandom for generating sharing keys.
     */
    private final SecureRandom secureRandom = new SecureRandom();

    @Override
    public String getShareKey() {
        byte[] bytes = new byte[33];
        secureRandom.nextBytes(bytes);
        return DatatypeConverter.printBase64Binary(bytes);
    }

}

@@ -889,49 +889,8 @@
 dom;
-%0Aimport javax.xml.bind.DatatypeConverter;
 %0A%0A/*
@@ -1099,16 +1099,45 @@
 nerator 
+extends SecureRandom%0A        
 implemen
@@ -1179,44 +1179,34 @@
   * 
-Instance of SecureRandom for
+The length of each
  generat
 ing 
@@ -1205,23 +1205,37 @@
 erat
-ing
+ed
  shar
-ing
+e
  key
+, in base64-digit
 s.%0A 
@@ -1257,60 +1257,864 @@
 ate 
-final SecureRandom secureRandom = new SecureRandom()
+static final int KEY_LENGTH = 44;%0A%0A    /**%0A     * The character representations of each possible base64 digit. This class%0A     * uses the URL-safe variant of base64 (also known as %22base64url%22), which%0A     * uses '-' and '_' instead of '+' and '/' for digits 62 and 63%0A     * respectively. See RFC 4648, Section 5: %22Base 64 Encoding with URL and%0A     * Filename Safe Alphabet%22 (https://tools.ietf.org/html/rfc4648#section-5).%0A     */%0A    private static final char%5B%5D URL_SAFE_BASE64_DIGITS = %7B%0A        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',%0A        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',%0A        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',%0A        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',%0A        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'%0A    %7D
 ;%0A%0A 
@@ -2160,24 +2160,25 @@
 Key() %7B%0A
+%0A
         
 byte%5B%5D b
@@ -2173,45 +2173,115 @@
     
-byte%5B%5D bytes = new byte%5B33
+// Produce storage space for required share key length%0A        char%5B%5D key = new char%5BKEY_LENGTH
 %5D;%0A
+%0A
         
 secu
@@ -2280,98 +2280,172 @@
     
-secureRandom.nextBytes(bytes);%0A        return DatatypeConverter.printBase64Binary(bytes
+// Fill key with random digits%0A        for (int i = 0; i %3C KEY_LENGTH; i++)%0A            key%5Bi%5D = URL_SAFE_BASE64_DIGITS%5Bnext(6)%5D;%0A%0A        return new String(key
 );%0A
+%0A
     

Merge url safety fix for share keys.