KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > io > crypt > KeyGenerator


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.io.crypt;
22
23 /**
24  * @exclude
25  */

26 public class KeyGenerator {
27     /**
28      * default constructor, creates an instance of KeyGenerator.
29      */

30     public KeyGenerator() {
31
32     }
33
34     /**
35      * generates an int[] array which has length four and produced according
36      * MD5 Message Digest Algorithm, as defined in RFC 1321.
37      *
38      * @param message -
39      * represents key to be converted as integer 128-bits<br>
40      * (will be used in XTEA encryption algorithm as key.)
41      * @return int array of size 128-bit.
42      */

43     public int[] core(String JavaDoc message) {
44
45         int[] messageAsInt = string2integer_padding(message);
46         int a = 0x01234567;
47         int b = 0x89abcdef;
48         int c = 0xfedcba98;
49         int d = 0x76543210;
50
51         for (int i = 0; i < messageAsInt.length; i += 16) {
52             int variable_a = a;
53             int variable_b = b;
54             int variable_c = c;
55             int variable_d = d;
56
57             a = ff(a, b, c, d, messageAsInt[i + 0], 7, 0xD76AA478);
58             d = ff(d, a, b, c, messageAsInt[i + 1], 12, 0xE8C7B756);
59             c = ff(c, d, a, b, messageAsInt[i + 2], 17, 0x242070DB);
60             b = ff(b, c, d, a, messageAsInt[i + 3], 22, 0xC1BDCEEE);
61             a = ff(a, b, c, d, messageAsInt[i + 4], 7, 0xF57C0FAF);
62             d = ff(d, a, b, c, messageAsInt[i + 5], 12, 0x4787C62A);
63             c = ff(c, d, a, b, messageAsInt[i + 6], 17, 0xA8304613);
64             b = ff(b, c, d, a, messageAsInt[i + 7], 22, 0xFD469501);
65             a = ff(a, b, c, d, messageAsInt[i + 8], 7, 0x698098D8);
66             d = ff(d, a, b, c, messageAsInt[i + 9], 12, 0x8B44F7AF);
67             c = ff(c, d, a, b, messageAsInt[i + 10], 17, 0xFFFF5BB1);
68             b = ff(b, c, d, a, messageAsInt[i + 11], 22, 0x895CD7BE);
69             a = ff(a, b, c, d, messageAsInt[i + 12], 7, 0x6B901122);
70             d = ff(d, a, b, c, messageAsInt[i + 13], 12, 0xFD987193);
71             c = ff(c, d, a, b, messageAsInt[i + 14], 17, 0xA679438E);
72             b = ff(b, c, d, a, messageAsInt[i + 15], 22, 0x49B40821);
73
74             a = gg(a, b, c, d, messageAsInt[i + 1], 5, 0xF61E2562);
75             d = gg(d, a, b, c, messageAsInt[i + 6], 9, 0xC040B340);
76             c = gg(c, d, a, b, messageAsInt[i + 11], 14, 0x265E5A51);
77             b = gg(b, c, d, a, messageAsInt[i + 0], 20, 0xE9B6C7AA);
78             a = gg(a, b, c, d, messageAsInt[i + 5], 5, 0xD62F105D);
79             d = gg(d, a, b, c, messageAsInt[i + 10], 9, 0x02441453);
80             c = gg(c, d, a, b, messageAsInt[i + 15], 14, 0xD8A1E681);
81             b = gg(b, c, d, a, messageAsInt[i + 4], 20, 0xE7D3FBC8);
82             a = gg(a, b, c, d, messageAsInt[i + 9], 5, 0x21E1CDE6);
83             d = gg(d, a, b, c, messageAsInt[i + 14], 9, 0xC33707D6);
84             c = gg(c, d, a, b, messageAsInt[i + 3], 14, 0xF4D50D87);
85             b = gg(b, c, d, a, messageAsInt[i + 8], 20, 0x455A14ED);
86             a = gg(a, b, c, d, messageAsInt[i + 13], 5, 0xA9E3E905);
87             d = gg(d, a, b, c, messageAsInt[i + 2], 9, 0xFCEFA3F8);
88             c = gg(c, d, a, b, messageAsInt[i + 7], 14, 0x676F02D9);
89             b = gg(b, c, d, a, messageAsInt[i + 12], 20, 0x8D2A4C8A);
90
91             a = hh(a, b, c, d, messageAsInt[i + 5], 4, 0xFFFA3942);
92             d = hh(d, a, b, c, messageAsInt[i + 8], 11, 0x8771F681);
93             c = hh(c, d, a, b, messageAsInt[i + 11], 16, 0x6D9D6122);
94             b = hh(b, c, d, a, messageAsInt[i + 14], 23, 0xFDE5380C);
95             a = hh(a, b, c, d, messageAsInt[i + 1], 4, 0xA4BEEA44);
96             d = hh(d, a, b, c, messageAsInt[i + 4], 11, 0x4BDECFA9);
97             c = hh(c, d, a, b, messageAsInt[i + 7], 16, 0xF6BB4B60);
98             b = hh(b, c, d, a, messageAsInt[i + 10], 23, 0xBEBFBC70);
99             a = hh(a, b, c, d, messageAsInt[i + 13], 4, 0x289B7EC6);
100             d = hh(d, a, b, c, messageAsInt[i + 0], 11, 0xEAA127FA);
101             c = hh(c, d, a, b, messageAsInt[i + 3], 16, 0xD4EF3085);
102             b = hh(b, c, d, a, messageAsInt[i + 6], 23, 0x04881D05);
103             a = hh(a, b, c, d, messageAsInt[i + 9], 4, 0xD9D4D039);
104             d = hh(d, a, b, c, messageAsInt[i + 12], 11, 0xE6DB99E5);
105             c = hh(c, d, a, b, messageAsInt[i + 15], 16, 0x1FA27CF8);
106             b = hh(b, c, d, a, messageAsInt[i + 2], 23, 0xC4AC5665);
107
108             a = ii(a, b, c, d, messageAsInt[i + 0], 6, 0xF4292244);
109             d = ii(d, a, b, c, messageAsInt[i + 7], 10, 0x432AFF97);
110             c = ii(c, d, a, b, messageAsInt[i + 14], 15, 0xAB9423A7);
111             b = ii(b, c, d, a, messageAsInt[i + 5], 21, 0xFC93A039);
112             a = ii(a, b, c, d, messageAsInt[i + 12], 6, 0x655B59C3);
113             d = ii(d, a, b, c, messageAsInt[i + 3], 10, 0x8F0CCC92);
114             c = ii(c, d, a, b, messageAsInt[i + 10], 15, 0xFFEFF47D);
115             b = ii(b, c, d, a, messageAsInt[i + 1], 21, 0x85845DD1);
116             a = ii(a, b, c, d, messageAsInt[i + 8], 6, 0x6FA87E4F);
117             d = ii(d, a, b, c, messageAsInt[i + 15], 10, 0xFE2CE6E0);
118             c = ii(c, d, a, b, messageAsInt[i + 6], 15, 0xA3014314);
119             b = ii(b, c, d, a, messageAsInt[i + 13], 21, 0x4E0811A1);
120             a = ii(a, b, c, d, messageAsInt[i + 4], 6, 0xF7537E82);
121             d = ii(d, a, b, c, messageAsInt[i + 11], 10, 0xBD3AF235);
122             c = ii(c, d, a, b, messageAsInt[i + 2], 15, 0x2AD7D2BB);
123             b = ii(b, c, d, a, messageAsInt[i + 9], 21, 0xEB86D391);
124
125             a = addInteger_wrappingAt32(a, variable_a);
126             b = addInteger_wrappingAt32(b, variable_b);
127             c = addInteger_wrappingAt32(c, variable_c);
128             d = addInteger_wrappingAt32(d, variable_d);
129         }
130         return new int[] { a, b, c, d };
131     }
132
133     /**
134      * core operation of MD5 Message Digest Algorithm
135      *
136      */

137     private int core_operation(int q, int a, int b, int x, int s, int t) {
138         return addInteger_wrappingAt32(bitwiseRotate32BitNumberLeft(
139                 addInteger_wrappingAt32(addInteger_wrappingAt32(a, q),
140                         addInteger_wrappingAt32(x, t)), s), b);
141     }
142
143     private int ff(int a, int b, int c, int d, int x, int s, int t) {
144         return core_operation(f(b, c, d), a, b, x, s, t);
145     }
146
147     /**
148      * nonlinear function, used in each round
149      *
150      */

151     private int f(int b, int c, int d) {
152         return (b & c) | ((~b) & d);
153     }
154
155     private int gg(int a, int b, int c, int d, int x, int s, int t) {
156         return core_operation(g(b, c, d), a, b, x, s, t);
157     }
158
159     /**
160      * nonlinear function, used in each round
161      *
162      */

163     private int g(int b, int c, int d) {
164         return (b & d) | (c & (~d));
165     }
166
167     private int hh(int a, int b, int c, int d, int x, int s, int t) {
168         return core_operation(h(b, c, d), a, b, x, s, t);
169     }
170
171     /**
172      * nonlinear function, used in each round
173      *
174      */

175     private int h(int b, int c, int d) {
176         return b ^ c ^ d;
177     }
178
179     private int ii(int a, int b, int c, int d, int x, int s, int t) {
180         return core_operation(i(b, c, d), a, b, x, s, t);
181     }
182
183     /**
184      * nonlinear function, used in each round
185      *
186      */

187     private int i(int b, int c, int d) {
188         return c ^ (b | (~d));
189     }
190
191     /**
192      * converts incoming padded String to an int array of size 128-bit.
193      *
194      * @param incomingString -
195      * String to be converted to an int array of size 128-bit.
196      * @return 128-bit int array
197      */

198     private int[] string2integer_padding(String JavaDoc incomingString) {
199         int i = 0;
200         int block = ((incomingString.length() + 8) >> 6) + 1;
201         int[] padded = new int[block * 16];
202         for (i = 0; i < block * 16; i++) {
203             padded[i] = 0;
204         }
205         for (i = 0; i < incomingString.length(); i++) {
206             padded[i >> 2] |= incomingString.charAt(i) << ((i % 4) * 8);
207         }
208         padded[i >> 2] |= 0x80 << ((i % 4) * 8);
209         padded[block * 16 - 2] = incomingString.length() * 8;
210
211         return padded;
212     }
213
214     private int addInteger_wrappingAt32(int x, int y) {
215         return ((x & 0x7FFFFFFF) + (y & 0x7FFFFFFF)) ^ (x & 0x80000000)
216                 ^ (y & 0x80000000);
217     }
218
219     private int bitwiseRotate32BitNumberLeft(int number, int p) {
220         return (number << p) | (number >>> (32 - p));
221     }
222
223 }
224
225
226
227
Popular Tags