001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.lang; 020 021import java.security.SecureRandom; 022 023public class RandomUtil { 024 public static final char[] CHARS=new char[]{ 025 '0','1','2','3','4','5','6','7','8','9', 026 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 027 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 028 029 public static final char[] CHARS_LC=new char[]{ 030 '0','1','2','3','4','5','6','7','8','9', 031 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 032 033 034 public static String createRandomString(int length){ 035 if(length<1) return ""; 036 SecureRandom sr = new SecureRandom(); 037 StringBuilder sb=new StringBuilder(); 038 for(int i=0;i<length;i++){ 039 int rnd=(int) (sr.nextDouble()*(CHARS.length-1)); 040 sb.append(CHARS[rnd]); 041 } 042 return sb.toString(); 043 } 044 045 public static String createRandomStringLC(int length){ 046 if(length<1) return ""; 047 SecureRandom sr = new SecureRandom(); 048 StringBuilder sb=new StringBuilder(); 049 for(int i=0;i<length;i++){ 050 int rnd=(int) (sr.nextDouble()*(CHARS_LC.length-1)); 051 sb.append(CHARS_LC[rnd]); 052 } 053 return sb.toString(); 054 } 055}