KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > keygenerator > JahiaKeyGen


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// JahiaKeyGen
15
// NK 18.04.2001
16
//
17
// NK 02.05.2001 // bug severe , utilisation de Random() car system.currentTimeMillis n'est pas al�atoire comme seed !!
18

19 //
20

21 package org.jahia.utils.keygenerator;
22
23 import java.security.MessageDigest JavaDoc;
24 import java.security.NoSuchAlgorithmException JavaDoc;
25 import java.util.Random JavaDoc;
26
27 import org.jahia.utils.Base64;
28 import org.jahia.utils.JahiaTools;
29
30
31 /**
32  * Random Key Generator
33  *
34  * @author NK
35  */

36
37
38 public class JahiaKeyGen
39 {
40
41     
42     /**
43      * The MersenneTwisterFast instance
44      *
45      **/

46     /*
47     private static MersenneTwisterFast mtfRandom = null;
48
49
50     static {
51         mtfRandom = new MersenneTwisterFast();
52     }
53     */

54
55     static String JavaDoc mLastKey = "";
56
57
58     //--------------------------------------------------------------------------
59
/**
60      * Return a random String key.
61      *
62      * @author NK
63      * @return String a random key
64      */

65     public static synchronized String JavaDoc getKey(){
66         
67         MersenneTwisterFast mtfRandom = new MersenneTwisterFast();
68         Random JavaDoc rd = new Random JavaDoc();
69         mtfRandom.setSeed(rd.nextLong());
70         String JavaDoc k = Long.toString(mtfRandom.nextLong());
71         while ( k.equals(mLastKey) || k.startsWith("-") ){
72             mtfRandom.setSeed(rd.nextLong());
73             k = Long.toString(mtfRandom.nextLong());
74         }
75         mLastKey = k;
76         return k;
77     }
78
79
80     //--------------------------------------------------------------------------
81
/**
82      * Return a random String key with initialization.
83      * Initalize the pseudo random number generator.
84      *
85      * @author NK
86      * @param long the seed
87      * @return String a random key
88      */

89     public static synchronized String JavaDoc getKey(long seed){
90         
91         MersenneTwisterFast mtfRandom = new MersenneTwisterFast();
92         mtfRandom.setSeed(seed);
93         String JavaDoc k = Long.toString(mtfRandom.nextLong());
94         while ( k.equals(mLastKey) || k.startsWith("-") ){
95             k = Long.toString(mtfRandom.nextLong());
96         }
97         mLastKey = k;
98         return k;
99     }
100
101
102     //--------------------------------------------------------------------------
103
/**
104      * Return a random String key limited to n characters.
105      *
106      * @author NK
107      * @return int number of char
108      * @return String a random key
109      */

110     public static synchronized String JavaDoc getKey(int n){
111         
112         MersenneTwisterFast mtfRandom = new MersenneTwisterFast();
113         Random JavaDoc rd = new Random JavaDoc();
114         mtfRandom.setSeed(rd.nextLong());
115         String JavaDoc k = encrypt(Long.toString(mtfRandom.nextLong()));
116         while ( k.equals(mLastKey) || k.startsWith("-") || (k.length()<n) ){
117             mtfRandom.setSeed(rd.nextLong());
118             k = encrypt(Long.toString(mtfRandom.nextLong()));
119         }
120         
121         String JavaDoc result = JahiaTools.replacePattern(k,"+","01");
122         result = JahiaTools.replacePattern(result,"/","10");
123         result = JahiaTools.replacePattern(result,"=","11");
124         
125         mLastKey = k;
126         return result.substring(0,n);
127
128     }
129
130
131     /**
132      * Encrypt a specified using the SHA algorithm.
133      *
134      * @param string to encrypt
135      *
136      * @return String the encrypted string or null on any failure
137      */

138     private static String JavaDoc encrypt (String JavaDoc data) {
139
140         if (data == null) {
141             return null;
142         }
143
144         if (data.length() == 0) {
145             return null;
146         }
147
148         String JavaDoc result = null;
149
150         try {
151             MessageDigest JavaDoc md = MessageDigest.getInstance ("SHA-1");
152             if (md != null)
153             {
154                 md.reset ();
155                 md.update (data.getBytes());
156                 result = new String JavaDoc(Base64.encode (md.digest()));
157             }
158             md = null;
159         }
160         catch (NoSuchAlgorithmException JavaDoc ex) {
161             result = null;
162         }
163
164         return result;
165     }
166
167
168     
169 }
Popular Tags