Guest User

Untitled

a guest
Dec 13th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package config;
  2.  
  3. /**
  4.  * The session identifier generator use to create unused Redis key, this is used by cookie system
  5.  * @author Deisss (LGPLv3)
  6.  * @version 0.1
  7.  */
  8. public class SessionIdentifierGenerator{
  9.     /**
  10.      * This function generate a unique -not used- key for Redis database storage.
  11.      * The function is mostly used during cookie creation. Use generate instead if you just
  12.      * need a unique string
  13.      * @return The key ready to be sended to Redis database
  14.      */
  15.     public static String create(){
  16.         Redis rd = Redis.getInstance();
  17.  
  18.         String key = SessionIdentifierGenerator.generate(32);
  19.         while(rd.getJedis().get(key) != null){
  20.             key = SessionIdentifierGenerator.generate(32);
  21.         }
  22.  
  23.         return key;
  24.     }
  25.  
  26.     /**
  27.      * This generate a random string of the given size
  28.      * @param length The output length to retrieve
  29.      * @return The generated string
  30.      */
  31.     public static String generate(int length){
  32.         String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  33.         int charLength = chars.length();
  34.         String pass = "";
  35.         for(int x=0; x<length; x++){
  36.             int i = (int) Math.floor(Math.random() * charLength);
  37.             pass += chars.charAt(i);
  38.         }
  39.         return pass;
  40.     }
  41. }
Add Comment
Please, Sign In to add comment