001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.server.string;
018
019import com.echothree.util.server.persistence.EncryptionUtils;
020import java.util.Random;
021
022public class KeyUtils {
023    
024    private KeyUtils() {
025        super();
026    }
027    
028    private static class KeyUtilsHolder {
029        static KeyUtils instance = new KeyUtils();
030    }
031    
032    public static KeyUtils getInstance() {
033        return KeyUtilsHolder.instance;
034    }
035    
036    private static final String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
037    private static final char []characterArray = characters.toCharArray();
038    private static final int characterCount = characters.length();
039    private static final int keyLength = 80;
040    
041    public String generateKey() {
042        StringBuilder keyBuilder = new StringBuilder(keyLength);
043        Random random = EncryptionUtils.getInstance().getRandom();
044        
045        for(int i = 0; i < keyLength; i++) {
046            keyBuilder.append(characterArray[random.nextInt(characterCount)]);
047        }
048        
049        return keyBuilder.toString();
050    }
051    
052}