KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ssh > Misc


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ssh;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.math.BigInteger JavaDoc;
16 import java.security.MessageDigest JavaDoc;
17 import java.security.NoSuchAlgorithmException JavaDoc;
18 import java.security.SecureRandom JavaDoc;
19
20 import org.eclipse.osgi.util.NLS;
21
22 class Misc {
23     private static MessageDigest JavaDoc md5 = null;
24     private static SecureRandom JavaDoc random = null;
25     private static long crc32_tab[] = {
26         0x00000000L,
27         0x77073096L,
28         0xee0e612cL,
29         0x990951baL,
30         0x076dc419L,
31         0x706af48fL,
32         0xe963a535L,
33         0x9e6495a3L,
34         0x0edb8832L,
35         0x79dcb8a4L,
36         0xe0d5e91eL,
37         0x97d2d988L,
38         0x09b64c2bL,
39         0x7eb17cbdL,
40         0xe7b82d07L,
41         0x90bf1d91L,
42         0x1db71064L,
43         0x6ab020f2L,
44         0xf3b97148L,
45         0x84be41deL,
46         0x1adad47dL,
47         0x6ddde4ebL,
48         0xf4d4b551L,
49         0x83d385c7L,
50         0x136c9856L,
51         0x646ba8c0L,
52         0xfd62f97aL,
53         0x8a65c9ecL,
54         0x14015c4fL,
55         0x63066cd9L,
56         0xfa0f3d63L,
57         0x8d080df5L,
58         0x3b6e20c8L,
59         0x4c69105eL,
60         0xd56041e4L,
61         0xa2677172L,
62         0x3c03e4d1L,
63         0x4b04d447L,
64         0xd20d85fdL,
65         0xa50ab56bL,
66         0x35b5a8faL,
67         0x42b2986cL,
68         0xdbbbc9d6L,
69         0xacbcf940L,
70         0x32d86ce3L,
71         0x45df5c75L,
72         0xdcd60dcfL,
73         0xabd13d59L,
74         0x26d930acL,
75         0x51de003aL,
76         0xc8d75180L,
77         0xbfd06116L,
78         0x21b4f4b5L,
79         0x56b3c423L,
80         0xcfba9599L,
81         0xb8bda50fL,
82         0x2802b89eL,
83         0x5f058808L,
84         0xc60cd9b2L,
85         0xb10be924L,
86         0x2f6f7c87L,
87         0x58684c11L,
88         0xc1611dabL,
89         0xb6662d3dL,
90         0x76dc4190L,
91         0x01db7106L,
92         0x98d220bcL,
93         0xefd5102aL,
94         0x71b18589L,
95         0x06b6b51fL,
96         0x9fbfe4a5L,
97         0xe8b8d433L,
98         0x7807c9a2L,
99         0x0f00f934L,
100         0x9609a88eL,
101         0xe10e9818L,
102         0x7f6a0dbbL,
103         0x086d3d2dL,
104         0x91646c97L,
105         0xe6635c01L,
106         0x6b6b51f4L,
107         0x1c6c6162L,
108         0x856530d8L,
109         0xf262004eL,
110         0x6c0695edL,
111         0x1b01a57bL,
112         0x8208f4c1L,
113         0xf50fc457L,
114         0x65b0d9c6L,
115         0x12b7e950L,
116         0x8bbeb8eaL,
117         0xfcb9887cL,
118         0x62dd1ddfL,
119         0x15da2d49L,
120         0x8cd37cf3L,
121         0xfbd44c65L,
122         0x4db26158L,
123         0x3ab551ceL,
124         0xa3bc0074L,
125         0xd4bb30e2L,
126         0x4adfa541L,
127         0x3dd895d7L,
128         0xa4d1c46dL,
129         0xd3d6f4fbL,
130         0x4369e96aL,
131         0x346ed9fcL,
132         0xad678846L,
133         0xda60b8d0L,
134         0x44042d73L,
135         0x33031de5L,
136         0xaa0a4c5fL,
137         0xdd0d7cc9L,
138         0x5005713cL,
139         0x270241aaL,
140         0xbe0b1010L,
141         0xc90c2086L,
142         0x5768b525L,
143         0x206f85b3L,
144         0xb966d409L,
145         0xce61e49fL,
146         0x5edef90eL,
147         0x29d9c998L,
148         0xb0d09822L,
149         0xc7d7a8b4L,
150         0x59b33d17L,
151         0x2eb40d81L,
152         0xb7bd5c3bL,
153         0xc0ba6cadL,
154         0xedb88320L,
155         0x9abfb3b6L,
156         0x03b6e20cL,
157         0x74b1d29aL,
158         0xead54739L,
159         0x9dd277afL,
160         0x04db2615L,
161         0x73dc1683L,
162         0xe3630b12L,
163         0x94643b84L,
164         0x0d6d6a3eL,
165         0x7a6a5aa8L,
166         0xe40ecf0bL,
167         0x9309ff9dL,
168         0x0a00ae27L,
169         0x7d079eb1L,
170         0xf00f9344L,
171         0x8708a3d2L,
172         0x1e01f268L,
173         0x6906c2feL,
174         0xf762575dL,
175         0x806567cbL,
176         0x196c3671L,
177         0x6e6b06e7L,
178         0xfed41b76L,
179         0x89d32be0L,
180         0x10da7a5aL,
181         0x67dd4accL,
182         0xf9b9df6fL,
183         0x8ebeeff9L,
184         0x17b7be43L,
185         0x60b08ed5L,
186         0xd6d6a3e8L,
187         0xa1d1937eL,
188         0x38d8c2c4L,
189         0x4fdff252L,
190         0xd1bb67f1L,
191         0xa6bc5767L,
192         0x3fb506ddL,
193         0x48b2364bL,
194         0xd80d2bdaL,
195         0xaf0a1b4cL,
196         0x36034af6L,
197         0x41047a60L,
198         0xdf60efc3L,
199         0xa867df55L,
200         0x316e8eefL,
201         0x4669be79L,
202         0xcb61b38cL,
203         0xbc66831aL,
204         0x256fd2a0L,
205         0x5268e236L,
206         0xcc0c7795L,
207         0xbb0b4703L,
208         0x220216b9L,
209         0x5505262fL,
210         0xc5ba3bbeL,
211         0xb2bd0b28L,
212         0x2bb45a92L,
213         0x5cb36a04L,
214         0xc2d7ffa7L,
215         0xb5d0cf31L,
216         0x2cd99e8bL,
217         0x5bdeae1dL,
218         0x9b64c2b0L,
219         0xec63f226L,
220         0x756aa39cL,
221         0x026d930aL,
222         0x9c0906a9L,
223         0xeb0e363fL,
224         0x72076785L,
225         0x05005713L,
226         0x95bf4a82L,
227         0xe2b87a14L,
228         0x7bb12baeL,
229         0x0cb61b38L,
230         0x92d28e9bL,
231         0xe5d5be0dL,
232         0x7cdcefb7L,
233         0x0bdbdf21L,
234         0x86d3d2d4L,
235         0xf1d4e242L,
236         0x68ddb3f8L,
237         0x1fda836eL,
238         0x81be16cdL,
239         0xf6b9265bL,
240         0x6fb077e1L,
241         0x18b74777L,
242         0x88085ae6L,
243         0xff0f6a70L,
244         0x66063bcaL,
245         0x11010b5cL,
246         0x8f659effL,
247         0xf862ae69L,
248         0x616bffd3L,
249         0x166ccf45L,
250         0xa00ae278L,
251         0xd70dd2eeL,
252         0x4e048354L,
253         0x3903b3c2L,
254         0xa7672661L,
255         0xd06016f7L,
256         0x4969474dL,
257         0x3e6e77dbL,
258         0xaed16a4aL,
259         0xd9d65adcL,
260         0x40df0b66L,
261         0x37d83bf0L,
262         0xa9bcae53L,
263         0xdebb9ec5L,
264         0x47b2cf7fL,
265         0x30b5ffe9L,
266         0xbdbdf21cL,
267         0xcabac28aL,
268         0x53b39330L,
269         0x24b4a3a6L,
270         0xbad03605L,
271         0xcdd70693L,
272         0x54de5729L,
273         0x23d967bfL,
274         0xb3667a2eL,
275         0xc4614ab8L,
276         0x5d681b02L,
277         0x2a6f2b94L,
278         0xb40bbe37L,
279         0xc30c8ea1L,
280         0x5a05df1bL,
281         0x2d02ef8dL
282     };
283 static public long crc32(byte[] b, int off, int len) {
284     return crc32(b, off, len, 0);
285 }
286 /**
287  * Compute the crc Cyclic Redundancy Check, with the polynomial 0xedb88320,
288  * The polynomial is X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
289  * We take it "backwards" and put the highest-order term in the lowest-order bit.
290  * The X^32 term is "implied"; the LSB is the X^31 term, etc.
291  * The X^0 term (usually shown as "+1") results in the MSB being 1.
292  * so the poly is 0x04c11db7 (used for Ethernet)
293  * The buf will be the Padding, Packet type, and Data fields.
294  * The crc is computed before any encryption.
295  * R =X^n * M rem P M message P polynomial crc R : crc calculated.
296  * T(x) = x^n * M(x) + R(x) property: T rem P = 0
297  */

298 static public long crc32(byte[] b, int off, int len, long crc32val) {
299     for (int i = 0; i < len; i++) {
300         crc32val = crc32_tab[(int) ((crc32val ^ b[off + i]) & 0xff)] ^ (crc32val >> 8);
301     }
302
303     return crc32val;
304 }
305 static public byte[] lengthEncode(byte[] b, int off, int len) {
306     byte[] result = new byte[len + 4];
307     writeInt(len, result, 0);
308     System.arraycopy(b, off, result, 4, len);
309     return result;
310 }
311 static public byte[] readMpInt(InputStream JavaDoc is) throws IOException JavaDoc {
312     int a = is.read();
313     int b = is.read();
314
315     if(a == -1 || b == -1){
316         throw new IOException JavaDoc(CVSSSHMessages.stream);
317     }
318
319     int bits = ((a & 0xFF) << 8) + (b & 0xFF);
320     int bytes = (bits + 7) / 8;
321
322     byte[] result = new byte[bytes];
323
324     readFully(is, result);
325
326     return result;
327 }
328 public static byte[] md5(byte[] b) throws IOException JavaDoc {
329     if (md5 == null) {
330         try {
331             md5 = MessageDigest.getInstance("MD5");//$NON-NLS-1$
332
} catch (NoSuchAlgorithmException JavaDoc e) {
333             throw new IOException JavaDoc(NLS.bind(CVSSSHMessages.Misc_missingMD5, new String JavaDoc[] { e.getMessage() }));
334         }
335     }
336
337     return md5.digest(b);
338 }
339 public static byte[] md5(String JavaDoc s) throws IOException JavaDoc {
340     return md5(s.getBytes());
341 }
342 public static void readFully(InputStream JavaDoc is, byte[] b) throws IOException JavaDoc {
343     readFully(is, b, 0, b.length);
344 }
345 public static void readFully(InputStream JavaDoc is, byte[] b, int off, int len) throws IOException JavaDoc {
346     int bytesRead = 0;
347     int totalBytesRead = 0;
348
349     while (totalBytesRead < len) {
350         bytesRead = is.read(b, totalBytesRead + off, len - totalBytesRead);
351
352         if (bytesRead == -1) {
353             throw new IOException JavaDoc(CVSSSHMessages.stream);
354         }
355
356         totalBytesRead += bytesRead;
357     }
358 }
359 public static int readInt(byte[] arr, int off) {
360     int a = arr[off] & 0xff;
361     int b = arr[off + 1] & 0xff;
362     int c = arr[off + 2] & 0xff;
363     int d = arr[off + 3] & 0xff;
364
365     return (a << 24) + (b << 16) + (c << 8) + d;
366 }
367 public static int readInt(InputStream JavaDoc is) throws IOException JavaDoc {
368     int a = is.read();
369     int b = is.read();
370     int c = is.read();
371     int d = is.read();
372
373     if (a == -1 || b == -1 || c == -1 || d == -1) {
374         throw new IOException JavaDoc(CVSSSHMessages.stream);
375     }
376
377     return (a << 24) + (b << 16) + (c << 8) + d;
378 }
379 public static String JavaDoc readString(InputStream JavaDoc is) throws IOException JavaDoc {
380     int size = Misc.readInt(is);
381     byte[] buffer = new byte[size];
382     for (int i = 0; i < buffer.length; i++) {
383         int next = is.read();
384         if (next == -1) {
385             throw new IOException JavaDoc(CVSSSHMessages.stream);
386         } else {
387             buffer[i] = (byte)next;
388         }
389     }
390     return new String JavaDoc(buffer);
391 }
392 public static void skipFully(InputStream JavaDoc is, long n) throws IOException JavaDoc {
393     while (n != 0) {
394         int b = is.read();
395
396         if (b == -1) {
397             if (n > 0) {
398                 throw new IOException JavaDoc(CVSSSHMessages.stream);
399             }
400
401             return;
402         }
403
404         --n;
405     }
406 }
407 public static void writeInt(int i, byte[] b, int off) {
408     b[off] = (byte) ((i >> 24) & 0xff);
409     b[off + 1] = (byte) ((i >> 16) & 0xff);
410     b[off + 2] = (byte) ((i >> 8) & 0xff);
411     b[off + 3] = (byte) (i & 0xff);
412 }
413 static public void xor(byte[] src_a, int off_a, byte[] src_b, int off_b, byte[] dst, int dst_off, int len) {
414     for(int i = 0; i < len; ++i){
415         dst[i + dst_off] = (byte) (src_a[i + off_a] ^ src_b[i + off_b]);
416     }
417 }
418 static public void random(byte[] b, int off, int len, boolean allowZeroBytes) {
419     if (random == null) {
420         try {
421             random = SecureRandom.getInstance("SHA1PRNG");//$NON-NLS-1$
422
} catch (NoSuchAlgorithmException JavaDoc e) {
423             // If SHA1PRNG is not available, just use the default
424
random = new SecureRandom JavaDoc();
425         }
426     }
427
428     for(int i = off; i < off + len; ++i){
429         do {
430             b[i] = (byte) random.nextInt();
431         } while(!allowZeroBytes && b[i] == 0);
432     }
433 }
434 static public byte[] encryptRSAPkcs1(byte[] data, byte[] public_key_exponent, byte[] public_key_modulus) {
435     byte[] block;
436
437     int offset = 0;
438     block = new byte[public_key_modulus.length];
439     block[offset++] = 0;
440     block[offset++] = 2;
441
442     Misc.random(block, offset, block.length - data.length - 3, false);
443     offset += block.length - data.length - 3;
444
445     block[offset++] = 0;
446
447     for (int i = 0; i < data.length; i++){
448         block[offset++] = data[i];
449     }
450
451     BigInteger JavaDoc m, e, message;
452     byte[] messageByte;
453
454     m = new BigInteger JavaDoc(1, public_key_modulus);
455     e = new BigInteger JavaDoc(1, public_key_exponent);
456     message = new BigInteger JavaDoc(1, block);
457     message = message.modPow(e, m);
458
459     byte[] messageByteTemp = message.toByteArray();
460     messageByte = new byte[public_key_modulus.length];
461
462     int tempOffset = 0;
463     while (messageByteTemp[tempOffset] == 0){
464         tempOffset++;
465     }
466
467     for (int i = messageByte.length - messageByteTemp.length + tempOffset; i < messageByte.length; i++){
468         messageByte[i] = messageByteTemp[tempOffset++];
469     }
470
471     return messageByte;
472 }
473 }
474
Popular Tags