Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2061 | es | 1 | package pt.estgp.es.exemplos.hibernate.utils; |
2 | |||
3 | |||
4 | import org.apache.commons.lang.RandomStringUtils; |
||
5 | import org.apache.log4j.Logger; |
||
6 | |||
7 | import java.io.*; |
||
8 | import java.math.BigInteger; |
||
9 | import java.rmi.dgc.VMID; |
||
10 | import java.security.MessageDigest; |
||
11 | import java.security.NoSuchAlgorithmException; |
||
12 | import java.util.Random; |
||
13 | |||
14 | /** |
||
15 | * |
||
16 | * @author Jorge Machado |
||
17 | */ |
||
18 | |||
19 | public class BytesUtils { |
||
20 | private static int counter = 0; |
||
21 | private static 1.5.0/docs/api/java/util/Random.html">Random random = new 1.5.0/docs/api/java/util/Random.html">Random(); |
||
22 | private static 1.5.0/docs/api/java/rmi/dgc/VMID.html">VMID vmid = new 1.5.0/docs/api/java/rmi/dgc/VMID.html">VMID(); |
||
23 | private static 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(BytesUtils.class); |
||
24 | |||
25 | /** Private Constructor */ |
||
26 | |||
27 | private BytesUtils () {} |
||
28 | |||
29 | |||
30 | public static 1.5.0/docs/api/java/lang/String.html">String getMd5FromFile(1.5.0/docs/api/java/lang/String.html">String filepath) |
||
31 | { |
||
32 | return getMD5(getBytes(filepath)); |
||
33 | } |
||
34 | |||
35 | public static byte[] getBytes(1.5.0/docs/api/java/lang/String.html">String fileName) |
||
36 | { |
||
37 | |||
38 | byte[] data = null; |
||
39 | try{ |
||
40 | |||
41 | 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream r = new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(fileName); |
||
42 | |||
43 | 1.5.0/docs/api/java/io/File.html">File f = new 1.5.0/docs/api/java/io/File.html">File(fileName); |
||
44 | |||
45 | //this value is never bigger than a integer |
||
46 | int len =(int) f.length(); |
||
47 | |||
48 | data = new byte[len]; |
||
49 | |||
50 | int bytesRead = 0; |
||
51 | |||
52 | bytesRead = r.read(data); |
||
53 | |||
54 | r.close(); |
||
55 | |||
56 | if(bytesRead == -1) |
||
57 | return null; |
||
58 | |||
59 | |||
60 | } |
||
61 | catch(1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e) |
||
62 | { |
||
63 | logger.error("Files getBytes()",e); |
||
64 | return null; |
||
65 | } |
||
66 | catch(1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
67 | { |
||
68 | logger.error("Files getData()",e); |
||
69 | return null; |
||
70 | } |
||
71 | return data; |
||
72 | } |
||
73 | |||
74 | public static 1.5.0/docs/api/java/lang/String.html">String getMD5(1.5.0/docs/api/java/lang/String.html">String data) |
||
75 | { |
||
76 | return getMD5(data.getBytes()); |
||
77 | } |
||
78 | |||
79 | public static 1.5.0/docs/api/java/lang/String.html">String getMD5(byte[] data) |
||
80 | { |
||
81 | return toHex(getMD5Bytes(data)); |
||
82 | } |
||
83 | |||
84 | public static 1.5.0/docs/api/java/lang/String.html">String getDigestMD5Hex(1.5.0/docs/api/java/lang/String.html">String str){ |
||
85 | byte[] digest = getDigestMD5(str.getBytes()); |
||
86 | if(digest != null) |
||
87 | return toHex(digest); |
||
88 | |||
89 | return null; |
||
90 | } |
||
91 | public static byte[] getDigestMD5(byte[] data) |
||
92 | { |
||
93 | byte[] result = null; |
||
94 | try{ |
||
95 | 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest md5 = 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest.getInstance("MD5"); |
||
96 | |||
97 | md5.update(data); |
||
98 | |||
99 | result = md5.digest(); |
||
100 | |||
101 | } |
||
102 | catch(1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e) |
||
103 | { |
||
104 | //this never hapens beacause MD5 is a correct algoritm |
||
105 | logger.fatal("MD5 function does not exists",e); |
||
106 | } |
||
107 | |||
108 | return result; |
||
109 | } |
||
110 | /********************************************************* |
||
111 | *Returns true if the two digests are equal |
||
112 | * |
||
113 | *******************************************************/ |
||
114 | public static boolean isDigestEqual(byte[] b1,byte[] b2) |
||
115 | { |
||
116 | |||
117 | try{ |
||
118 | 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest md5 = 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest.getInstance("MD5"); |
||
119 | |||
120 | return md5.isEqual(b1,b2); |
||
121 | |||
122 | } |
||
123 | catch(1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e) |
||
124 | { |
||
125 | //this never hapens beacause MD5 is a correct algoritm |
||
126 | logger.fatal("MD5 function does not exists",e); |
||
127 | } |
||
128 | |||
129 | return false; |
||
130 | |||
131 | } |
||
132 | |||
133 | public static byte[] getMD5Bytes(byte[] data) |
||
134 | { |
||
135 | try |
||
136 | { |
||
137 | 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest digest = 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest.getInstance("MD5"); |
||
138 | |||
139 | return digest.digest(data); |
||
140 | } |
||
141 | catch (1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e) |
||
142 | { |
||
143 | logger.fatal("MD5 function does not exists",e); |
||
144 | } |
||
145 | return null; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Return a hex representation of the byte array |
||
150 | * |
||
151 | * @param data The data to transform. |
||
152 | * @return A hex representation of the data. |
||
153 | */ |
||
154 | public static 1.5.0/docs/api/java/lang/String.html">String toHex(byte[] data) |
||
155 | { |
||
156 | if ((data == null) || (data.length == 0)) |
||
157 | return null; |
||
158 | |||
159 | 1.5.0/docs/api/java/lang/StringBuffer.html">StringBuffer result = new 1.5.0/docs/api/java/lang/StringBuffer.html">StringBuffer(); |
||
160 | |||
161 | // This is far from the most efficient way to do things... |
||
162 | for (int i = 0; i < data.length; i++) |
||
163 | { |
||
164 | int low = (int) (data[i] & 0x0F); |
||
165 | int high = (int) (data[i] & 0xF0); |
||
166 | |||
167 | result.append(1.5.0/docs/api/java/lang/Integer.html">Integer.toHexString(high).substring(0, 1)); |
||
168 | result.append(1.5.0/docs/api/java/lang/Integer.html">Integer.toHexString(low)); |
||
169 | } |
||
170 | |||
171 | return result.toString(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Generate a unique key. |
||
176 | * The key is a long (length 38 to 40) sequence of digits. |
||
177 | * |
||
178 | * @return A unique key as a long sequence of base-10 digits. |
||
179 | */ |
||
180 | public static 1.5.0/docs/api/java/lang/String.html">String generateKey() |
||
181 | { |
||
182 | return new 1.5.0/docs/api/java/math/BigInteger.html">BigInteger(generateBytesKey()).abs().toString(); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Generate a unique key. |
||
187 | * The key is a 32-character long sequence of hex digits. |
||
188 | * |
||
189 | * @return A unique key as a long sequence of hex digits. |
||
190 | */ |
||
191 | public static 1.5.0/docs/api/java/lang/String.html">String generateHexKey() |
||
192 | { |
||
193 | return toHex(generateBytesKey()); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Generate a unique key as a byte array. |
||
198 | * |
||
199 | * @return A unique key as a byte array. |
||
200 | */ |
||
201 | public static synchronized byte[] generateBytesKey() |
||
202 | { |
||
203 | byte[] junk = new byte[16]; |
||
204 | |||
205 | random.nextBytes(junk); |
||
206 | |||
207 | 1.5.0/docs/api/java/lang/String.html">String input = new 1.5.0/docs/api/java/lang/StringBuffer.html">StringBuffer().append(vmid).append(new java.util.5+0%2Fdocs%2Fapi+Date">Date()).append(junk).append(counter++).toString(); |
||
208 | |||
209 | return getMD5Bytes(input.getBytes()); |
||
210 | } |
||
211 | |||
212 | // The following two methods are taken from the Jakarta IOUtil class. |
||
213 | |||
214 | /** |
||
215 | * Copy stream-data from source to destination. This method does not |
||
216 | * buffer, flush or close the streams, as to do so would require making |
||
217 | * non-portable assumptions about the streams' origin and further use. If |
||
218 | * you wish to perform a buffered copy, use {@link #bufferedCopy}. |
||
219 | * |
||
220 | * @param input The InputStream to obtain data from. |
||
221 | * @param output The OutputStream to copy data to. |
||
222 | */ |
||
223 | public static void copy( final 5+0%2Fdocs%2Fapi+InputStream">InputStream input, final 5+0%2Fdocs%2Fapi+OutputStream">OutputStream output ) |
||
224 | throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
225 | { |
||
226 | final int BUFFER_SIZE = 1024 * 4; |
||
227 | final byte[] buffer = new byte[ BUFFER_SIZE ]; |
||
228 | |||
229 | while( true ) |
||
230 | { |
||
231 | final int count = input.read( buffer, 0, BUFFER_SIZE ); |
||
232 | if( -1 == count ) break; |
||
233 | |||
234 | // write out those same bytes |
||
235 | output.write( buffer, 0, count ); |
||
236 | } |
||
237 | |||
238 | //needed to flush cache |
||
239 | //output.flush(); |
||
240 | } |
||
241 | |||
242 | |||
243 | public static void bufferedCopy( final 5+0%2Fdocs%2Fapi+InputStream">InputStream source, final 5+0%2Fdocs%2Fapi+OutputStream">OutputStream destination ) |
||
244 | throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
245 | { |
||
246 | final 1.5.0/docs/api/java/io/BufferedInputStream.html">BufferedInputStream input = new 1.5.0/docs/api/java/io/BufferedInputStream.html">BufferedInputStream( source ); |
||
247 | final 1.5.0/docs/api/java/io/BufferedOutputStream.html">BufferedOutputStream output = new 1.5.0/docs/api/java/io/BufferedOutputStream.html">BufferedOutputStream( destination ); |
||
248 | copy( input, output ); |
||
249 | output.flush(); |
||
250 | } |
||
251 | |||
252 | public static int gen5DigitsKey() { |
||
253 | 1.5.0/docs/api/java/util/Random.html">Random r = new 1.5.0/docs/api/java/util/Random.html">Random( 1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis() ); |
||
254 | return 10000 + r.nextInt(20000); |
||
255 | } |
||
256 | |||
257 | public static 1.5.0/docs/api/java/lang/String.html">String genCharsDigitsKey(int number) { |
||
258 | 1.5.0/docs/api/java/lang/String.html">String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()-_=+[{]},<.>?"; |
||
259 | return RandomStringUtils.random(number, characters); |
||
260 | } |
||
261 | |||
262 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) |
||
263 | { |
||
264 | 1.5.0/docs/api/java/lang/System.html">System.out.println(gen5DigitsKey()); |
||
265 | } |
||
266 | |||
267 | |||
268 | } |