View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License ,
7   Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS ,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND ,
18  either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  package org.apache.myfaces.tobago.internal.util;
24  
25  import java.security.SecureRandom;
26  import java.util.Base64;
27  
28  /**
29   * Helps to get a random string.
30   */
31  public class RandomUtils {
32  
33    private static final SecureRandom RANDOM = new SecureRandom();
34  
35    private static final int SECRET_LENGTH = 16;
36  
37    private static String encodeBase64(final byte[] bytes) {
38      return Base64.getEncoder().encodeToString(bytes);
39    }
40  
41    public static String nextString() {
42      final byte[] bytes = new byte[SECRET_LENGTH];
43      RANDOM.nextBytes(bytes);
44      return encodeBase64(bytes);
45    }
46  
47  }