001: package org.apache.turbine.services.crypto.impl;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Random;
023:
024: /**
025: * Unix crypt (3) algorithm implementation. The java
026: * implementation was taken from the JetSpeed Portal project
027: * (see org.apache.jetspeed.services.security.ldap.UnixCrypt).
028: *
029: * @author Siegfried Goeschl
030: */
031: public class UnixCrypt {
032: private static final char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
033: .toCharArray();
034: private static final int ITERATIONS = 16;
035: private static final int con_salt[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0,
036: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
037: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,
038: 3, 4, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10, 11, 12, 13,
039: 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
040: 29, 30, 31, 32, 33, 34, 35, 36, 37, 32, 33, 34, 35, 36, 37,
041: 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
042: 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 0, 0, 0, 0 };
043: private static final boolean shifts2[] = { false, false, true,
044: true, true, true, true, true, false, true, true, true,
045: true, true, true, false };
046: private static final int skb[][] = {
047: { 0, 16, 0x20000000, 0x20000010, 0x10000, 0x10010,
048: 0x20010000, 0x20010010, 2048, 2064, 0x20000800,
049: 0x20000810, 0x10800, 0x10810, 0x20010800,
050: 0x20010810, 32, 48, 0x20000020, 0x20000030,
051: 0x10020, 0x10030, 0x20010020, 0x20010030, 2080,
052: 2096, 0x20000820, 0x20000830, 0x10820, 0x10830,
053: 0x20010820, 0x20010830, 0x80000, 0x80010,
054: 0x20080000, 0x20080010, 0x90000, 0x90010,
055: 0x20090000, 0x20090010, 0x80800, 0x80810,
056: 0x20080800, 0x20080810, 0x90800, 0x90810,
057: 0x20090800, 0x20090810, 0x80020, 0x80030,
058: 0x20080020, 0x20080030, 0x90020, 0x90030,
059: 0x20090020, 0x20090030, 0x80820, 0x80830,
060: 0x20080820, 0x20080830, 0x90820, 0x90830,
061: 0x20090820, 0x20090830 },
062: { 0, 0x2000000, 8192, 0x2002000, 0x200000, 0x2200000,
063: 0x202000, 0x2202000, 4, 0x2000004, 8196, 0x2002004,
064: 0x200004, 0x2200004, 0x202004, 0x2202004, 1024,
065: 0x2000400, 9216, 0x2002400, 0x200400, 0x2200400,
066: 0x202400, 0x2202400, 1028, 0x2000404, 9220,
067: 0x2002404, 0x200404, 0x2200404, 0x202404,
068: 0x2202404, 0x10000000, 0x12000000, 0x10002000,
069: 0x12002000, 0x10200000, 0x12200000, 0x10202000,
070: 0x12202000, 0x10000004, 0x12000004, 0x10002004,
071: 0x12002004, 0x10200004, 0x12200004, 0x10202004,
072: 0x12202004, 0x10000400, 0x12000400, 0x10002400,
073: 0x12002400, 0x10200400, 0x12200400, 0x10202400,
074: 0x12202400, 0x10000404, 0x12000404, 0x10002404,
075: 0x12002404, 0x10200404, 0x12200404, 0x10202404,
076: 0x12202404 },
077: { 0, 1, 0x40000, 0x40001, 0x1000000, 0x1000001, 0x1040000,
078: 0x1040001, 2, 3, 0x40002, 0x40003, 0x1000002,
079: 0x1000003, 0x1040002, 0x1040003, 512, 513, 0x40200,
080: 0x40201, 0x1000200, 0x1000201, 0x1040200,
081: 0x1040201, 514, 515, 0x40202, 0x40203, 0x1000202,
082: 0x1000203, 0x1040202, 0x1040203, 0x8000000,
083: 0x8000001, 0x8040000, 0x8040001, 0x9000000,
084: 0x9000001, 0x9040000, 0x9040001, 0x8000002,
085: 0x8000003, 0x8040002, 0x8040003, 0x9000002,
086: 0x9000003, 0x9040002, 0x9040003, 0x8000200,
087: 0x8000201, 0x8040200, 0x8040201, 0x9000200,
088: 0x9000201, 0x9040200, 0x9040201, 0x8000202,
089: 0x8000203, 0x8040202, 0x8040203, 0x9000202,
090: 0x9000203, 0x9040202, 0x9040203 },
091: { 0, 0x100000, 256, 0x100100, 8, 0x100008, 264, 0x100108,
092: 4096, 0x101000, 4352, 0x101100, 4104, 0x101008,
093: 4360, 0x101108, 0x4000000, 0x4100000, 0x4000100,
094: 0x4100100, 0x4000008, 0x4100008, 0x4000108,
095: 0x4100108, 0x4001000, 0x4101000, 0x4001100,
096: 0x4101100, 0x4001008, 0x4101008, 0x4001108,
097: 0x4101108, 0x20000, 0x120000, 0x20100, 0x120100,
098: 0x20008, 0x120008, 0x20108, 0x120108, 0x21000,
099: 0x121000, 0x21100, 0x121100, 0x21008, 0x121008,
100: 0x21108, 0x121108, 0x4020000, 0x4120000, 0x4020100,
101: 0x4120100, 0x4020008, 0x4120008, 0x4020108,
102: 0x4120108, 0x4021000, 0x4121000, 0x4021100,
103: 0x4121100, 0x4021008, 0x4121008, 0x4021108,
104: 0x4121108 },
105: { 0, 0x10000000, 0x10000, 0x10010000, 4, 0x10000004,
106: 0x10004, 0x10010004, 0x20000000, 0x30000000,
107: 0x20010000, 0x30010000, 0x20000004, 0x30000004,
108: 0x20010004, 0x30010004, 0x100000, 0x10100000,
109: 0x110000, 0x10110000, 0x100004, 0x10100004,
110: 0x110004, 0x10110004, 0x20100000, 0x30100000,
111: 0x20110000, 0x30110000, 0x20100004, 0x30100004,
112: 0x20110004, 0x30110004, 4096, 0x10001000, 0x11000,
113: 0x10011000, 4100, 0x10001004, 0x11004, 0x10011004,
114: 0x20001000, 0x30001000, 0x20011000, 0x30011000,
115: 0x20001004, 0x30001004, 0x20011004, 0x30011004,
116: 0x101000, 0x10101000, 0x111000, 0x10111000,
117: 0x101004, 0x10101004, 0x111004, 0x10111004,
118: 0x20101000, 0x30101000, 0x20111000, 0x30111000,
119: 0x20101004, 0x30101004, 0x20111004, 0x30111004 },
120: { 0, 0x8000000, 8, 0x8000008, 1024, 0x8000400, 1032,
121: 0x8000408, 0x20000, 0x8020000, 0x20008, 0x8020008,
122: 0x20400, 0x8020400, 0x20408, 0x8020408, 1,
123: 0x8000001, 9, 0x8000009, 1025, 0x8000401, 1033,
124: 0x8000409, 0x20001, 0x8020001, 0x20009, 0x8020009,
125: 0x20401, 0x8020401, 0x20409, 0x8020409, 0x2000000,
126: 0xa000000, 0x2000008, 0xa000008, 0x2000400,
127: 0xa000400, 0x2000408, 0xa000408, 0x2020000,
128: 0xa020000, 0x2020008, 0xa020008, 0x2020400,
129: 0xa020400, 0x2020408, 0xa020408, 0x2000001,
130: 0xa000001, 0x2000009, 0xa000009, 0x2000401,
131: 0xa000401, 0x2000409, 0xa000409, 0x2020001,
132: 0xa020001, 0x2020009, 0xa020009, 0x2020401,
133: 0xa020401, 0x2020409, 0xa020409 },
134: { 0, 256, 0x80000, 0x80100, 0x1000000, 0x1000100,
135: 0x1080000, 0x1080100, 16, 272, 0x80010, 0x80110,
136: 0x1000010, 0x1000110, 0x1080010, 0x1080110,
137: 0x200000, 0x200100, 0x280000, 0x280100, 0x1200000,
138: 0x1200100, 0x1280000, 0x1280100, 0x200010,
139: 0x200110, 0x280010, 0x280110, 0x1200010, 0x1200110,
140: 0x1280010, 0x1280110, 512, 768, 0x80200, 0x80300,
141: 0x1000200, 0x1000300, 0x1080200, 0x1080300, 528,
142: 784, 0x80210, 0x80310, 0x1000210, 0x1000310,
143: 0x1080210, 0x1080310, 0x200200, 0x200300, 0x280200,
144: 0x280300, 0x1200200, 0x1200300, 0x1280200,
145: 0x1280300, 0x200210, 0x200310, 0x280210, 0x280310,
146: 0x1200210, 0x1200310, 0x1280210, 0x1280310 },
147: { 0, 0x4000000, 0x40000, 0x4040000, 2, 0x4000002, 0x40002,
148: 0x4040002, 8192, 0x4002000, 0x42000, 0x4042000,
149: 8194, 0x4002002, 0x42002, 0x4042002, 32, 0x4000020,
150: 0x40020, 0x4040020, 34, 0x4000022, 0x40022,
151: 0x4040022, 8224, 0x4002020, 0x42020, 0x4042020,
152: 8226, 0x4002022, 0x42022, 0x4042022, 2048,
153: 0x4000800, 0x40800, 0x4040800, 2050, 0x4000802,
154: 0x40802, 0x4040802, 10240, 0x4002800, 0x42800,
155: 0x4042800, 10242, 0x4002802, 0x42802, 0x4042802,
156: 2080, 0x4000820, 0x40820, 0x4040820, 2082,
157: 0x4000822, 0x40822, 0x4040822, 10272, 0x4002820,
158: 0x42820, 0x4042820, 10274, 0x4002822, 0x42822,
159: 0x4042822 } };
160: private static final int SPtrans[][] = {
161: { 0x820200, 0x20000, 0x80800000, 0x80820200, 0x800000,
162: 0x80020200, 0x80020000, 0x80800000, 0x80020200,
163: 0x820200, 0x820000, 0x80000200, 0x80800200,
164: 0x800000, 0, 0x80020000, 0x20000, 0x80000000,
165: 0x800200, 0x20200, 0x80820200, 0x820000,
166: 0x80000200, 0x800200, 0x80000000, 512, 0x20200,
167: 0x80820000, 512, 0x80800200, 0x80820000, 0, 0,
168: 0x80820200, 0x800200, 0x80020000, 0x820200,
169: 0x20000, 0x80000200, 0x800200, 0x80820000, 512,
170: 0x20200, 0x80800000, 0x80020200, 0x80000000,
171: 0x80800000, 0x820000, 0x80820200, 0x20200,
172: 0x820000, 0x80800200, 0x800000, 0x80000200,
173: 0x80020000, 0, 0x20000, 0x800000, 0x80800200,
174: 0x820200, 0x80000000, 0x80820000, 512, 0x80020200 },
175: { 0x10042004, 0, 0x42000, 0x10040000, 0x10000004, 8196,
176: 0x10002000, 0x42000, 8192, 0x10040004, 4,
177: 0x10002000, 0x40004, 0x10042000, 0x10040000, 4,
178: 0x40000, 0x10002004, 0x10040004, 8192, 0x42004,
179: 0x10000000, 0, 0x40004, 0x10002004, 0x42004,
180: 0x10042000, 0x10000004, 0x10000000, 0x40000, 8196,
181: 0x10042004, 0x40004, 0x10042000, 0x10002000,
182: 0x42004, 0x10042004, 0x40004, 0x10000004, 0,
183: 0x10000000, 8196, 0x40000, 0x10040004, 8192,
184: 0x10000000, 0x42004, 0x10002004, 0x10042000, 8192,
185: 0, 0x10000004, 4, 0x10042004, 0x42000, 0x10040000,
186: 0x10040004, 0x40000, 8196, 0x10002000, 0x10002004,
187: 4, 0x10040000, 0x42000 },
188: { 0x41000000, 0x1010040, 64, 0x41000040, 0x40010000,
189: 0x1000000, 0x41000040, 0x10040, 0x1000040, 0x10000,
190: 0x1010000, 0x40000000, 0x41010040, 0x40000040,
191: 0x40000000, 0x41010000, 0, 0x40010000, 0x1010040,
192: 64, 0x40000040, 0x41010040, 0x10000, 0x41000000,
193: 0x41010000, 0x1000040, 0x40010040, 0x1010000,
194: 0x10040, 0, 0x1000000, 0x40010040, 0x1010040, 64,
195: 0x40000000, 0x10000, 0x40000040, 0x40010000,
196: 0x1010000, 0x41000040, 0, 0x1010040, 0x10040,
197: 0x41010000, 0x40010000, 0x1000000, 0x41010040,
198: 0x40000000, 0x40010040, 0x41000000, 0x1000000,
199: 0x41010040, 0x10000, 0x1000040, 0x41000040,
200: 0x10040, 0x1000040, 0, 0x41010000, 0x40000040,
201: 0x41000000, 0x40010040, 64, 0x1010000 },
202: { 0x100402, 0x4000400, 2, 0x4100402, 0, 0x4100000,
203: 0x4000402, 0x100002, 0x4100400, 0x4000002,
204: 0x4000000, 1026, 0x4000002, 0x100402, 0x100000,
205: 0x4000000, 0x4100002, 0x100400, 1024, 2, 0x100400,
206: 0x4000402, 0x4100000, 1024, 1026, 0, 0x100002,
207: 0x4100400, 0x4000400, 0x4100002, 0x4100402,
208: 0x100000, 0x4100002, 1026, 0x100000, 0x4000002,
209: 0x100400, 0x4000400, 2, 0x4100000, 0x4000402, 0,
210: 1024, 0x100002, 0, 0x4100002, 0x4100400, 1024,
211: 0x4000000, 0x4100402, 0x100402, 0x100000,
212: 0x4100402, 2, 0x4000400, 0x100402, 0x100002,
213: 0x100400, 0x4100000, 0x4000402, 1026, 0x4000000,
214: 0x4000002, 0x4100400 },
215: { 0x2000000, 16384, 256, 0x2004108, 0x2004008, 0x2000100,
216: 16648, 0x2004000, 16384, 8, 0x2000008, 16640,
217: 0x2000108, 0x2004008, 0x2004100, 0, 16640,
218: 0x2000000, 16392, 264, 0x2000100, 16648, 0,
219: 0x2000008, 8, 0x2000108, 0x2004108, 16392,
220: 0x2004000, 256, 264, 0x2004100, 0x2004100,
221: 0x2000108, 16392, 0x2004000, 16384, 8, 0x2000008,
222: 0x2000100, 0x2000000, 16640, 0x2004108, 0, 16648,
223: 0x2000000, 256, 16392, 0x2000108, 256, 0,
224: 0x2004108, 0x2004008, 0x2004100, 264, 16384, 16640,
225: 0x2004008, 0x2000100, 264, 8, 16648, 0x2004000,
226: 0x2000008 },
227: { 0x20000010, 0x80010, 0, 0x20080800, 0x80010, 2048,
228: 0x20000810, 0x80000, 2064, 0x20080810, 0x80800,
229: 0x20000000, 0x20000800, 0x20000010, 0x20080000,
230: 0x80810, 0x80000, 0x20000810, 0x20080010, 0, 2048,
231: 16, 0x20080800, 0x20080010, 0x20080810, 0x20080000,
232: 0x20000000, 2064, 16, 0x80800, 0x80810, 0x20000800,
233: 2064, 0x20000000, 0x20000800, 0x80810, 0x20080800,
234: 0x80010, 0, 0x20000800, 0x20000000, 2048,
235: 0x20080010, 0x80000, 0x80010, 0x20080810, 0x80800,
236: 16, 0x20080810, 0x80800, 0x80000, 0x20000810,
237: 0x20000010, 0x20080000, 0x80810, 0, 2048,
238: 0x20000010, 0x20000810, 0x20080800, 0x20080000,
239: 2064, 16, 0x20080010 },
240: { 4096, 128, 0x400080, 0x400001, 0x401081, 4097, 4224, 0,
241: 0x400000, 0x400081, 129, 0x401000, 1, 0x401080,
242: 0x401000, 129, 0x400081, 4096, 4097, 0x401081, 0,
243: 0x400080, 0x400001, 4224, 0x401001, 4225, 0x401080,
244: 1, 4225, 0x401001, 128, 0x400000, 4225, 0x401000,
245: 0x401001, 129, 4096, 128, 0x400000, 0x401001,
246: 0x400081, 4225, 4224, 0, 128, 0x400001, 1,
247: 0x400080, 0, 0x400081, 0x400080, 4224, 129, 4096,
248: 0x401081, 0x400000, 0x401080, 1, 4097, 0x401081,
249: 0x400001, 0x401080, 0x401000, 4097 },
250: { 0x8200020, 0x8208000, 32800, 0, 0x8008000, 0x200020,
251: 0x8200000, 0x8208020, 32, 0x8000000, 0x208000,
252: 32800, 0x208020, 0x8008020, 0x8000020, 0x8200000,
253: 32768, 0x208020, 0x200020, 0x8008000, 0x8208020,
254: 0x8000020, 0, 0x208000, 0x8000000, 0x200000,
255: 0x8008020, 0x8200020, 0x200000, 32768, 0x8208000,
256: 32, 0x200000, 32768, 0x8000020, 0x8208020, 32800,
257: 0x8000000, 0, 0x208000, 0x8200020, 0x8008020,
258: 0x8008000, 0x200020, 0x8208000, 32, 0x200020,
259: 0x8008000, 0x8208020, 0x200000, 0x8200000,
260: 0x8000020, 0x208000, 32800, 0x8008020, 0x8200000,
261: 32, 0x8208000, 0x208020, 0, 0x8000000, 0x8200020,
262: 32768, 0x208020 } };
263: private static final int cov_2char[] = { 46, 47, 48, 49, 50, 51,
264: 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73,
265: 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
266: 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
267: 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
268: 120, 121, 122 };
269:
270: private UnixCrypt() {
271: }
272:
273: private static final int D_ENCRYPT(int L, int R, int S, int E0,
274: int E1, int s[]) {
275: int v = R ^ R >>> 16;
276: int u = v & E0;
277: v &= E1;
278: u = u ^ u << 16 ^ R ^ s[S];
279: int t = v ^ v << 16 ^ R ^ s[S + 1];
280: t = t >>> 4 | t << 28;
281: L ^= SPtrans[1][t & 0x3f] | SPtrans[3][t >>> 8 & 0x3f]
282: | SPtrans[5][t >>> 16 & 0x3f]
283: | SPtrans[7][t >>> 24 & 0x3f] | SPtrans[0][u & 0x3f]
284: | SPtrans[2][u >>> 8 & 0x3f]
285: | SPtrans[4][u >>> 16 & 0x3f]
286: | SPtrans[6][u >>> 24 & 0x3f];
287: return L;
288: }
289:
290: private static final int HPERM_OP(int a, int n, int m) {
291: int t = (a << 16 - n ^ a) & m;
292: a = a ^ t ^ t >>> 16 - n;
293: return a;
294: }
295:
296: private static final void PERM_OP(int a, int b, int n, int m,
297: int results[]) {
298: int t = (a >>> n ^ b) & m;
299: a ^= t << n;
300: b ^= t;
301: results[0] = a;
302: results[1] = b;
303: }
304:
305: private static final int[] body(int schedule[], int Eswap0,
306: int Eswap1) {
307: int left = 0;
308: int right = 0;
309: int t = 0;
310: for (int j = 0; j < 25; j++) {
311: for (int i = 0; i < 32; i += 4) {
312: left = D_ENCRYPT(left, right, i, Eswap0, Eswap1,
313: schedule);
314: right = D_ENCRYPT(right, left, i + 2, Eswap0, Eswap1,
315: schedule);
316: }
317:
318: t = left;
319: left = right;
320: right = t;
321: }
322:
323: t = right;
324: right = left >>> 1 | left << 31;
325: left = t >>> 1 | t << 31;
326: left &= 0xffffffff;
327: right &= 0xffffffff;
328: int results[] = new int[2];
329: PERM_OP(right, left, 1, 0x55555555, results);
330: right = results[0];
331: left = results[1];
332: PERM_OP(left, right, 8, 0xff00ff, results);
333: left = results[0];
334: right = results[1];
335: PERM_OP(right, left, 2, 0x33333333, results);
336: right = results[0];
337: left = results[1];
338: PERM_OP(left, right, 16, 65535, results);
339: left = results[0];
340: right = results[1];
341: PERM_OP(right, left, 4, 0xf0f0f0f, results);
342: right = results[0];
343: left = results[1];
344: int out[] = new int[2];
345: out[0] = left;
346: out[1] = right;
347: return out;
348: }
349:
350: private static final int byteToUnsigned(byte b) {
351: int value = b;
352: return value < 0 ? value + 256 : value;
353: }
354:
355: public static final String crypt(String original) {
356: Random randomGenerator = new Random();
357: int numSaltChars = saltChars.length;
358: String salt = ""
359: + saltChars[Math.abs(randomGenerator.nextInt())
360: % numSaltChars]
361: + saltChars[Math.abs(randomGenerator.nextInt())
362: % numSaltChars];
363: return crypt(salt, original);
364: }
365:
366: public static final String crypt(String salt, String original) {
367: for (; salt.length() < 2; salt = salt + "A")
368: ;
369: StringBuffer buffer = new StringBuffer(" ");
370: char charZero = salt.charAt(0);
371: char charOne = salt.charAt(1);
372: buffer.setCharAt(0, charZero);
373: buffer.setCharAt(1, charOne);
374: int Eswap0 = con_salt[charZero];
375: int Eswap1 = con_salt[charOne] << 4;
376: byte key[] = new byte[8];
377: for (int i = 0; i < key.length; i++)
378: key[i] = 0;
379:
380: for (int i = 0; i < key.length && i < original.length(); i++) {
381: int iChar = original.charAt(i);
382: key[i] = (byte) (iChar << 1);
383: }
384:
385: int schedule[] = des_set_key(key);
386: int out[] = body(schedule, Eswap0, Eswap1);
387: byte b[] = new byte[9];
388: intToFourBytes(out[0], b, 0);
389: intToFourBytes(out[1], b, 4);
390: b[8] = 0;
391: int i = 2;
392: int y = 0;
393: int u = 128;
394: for (; i < 13; i++) {
395: int j = 0;
396: int c = 0;
397: for (; j < 6; j++) {
398: c <<= 1;
399: if ((b[y] & u) != 0)
400: c |= 0x1;
401: u >>>= 1;
402: if (u == 0) {
403: y++;
404: u = 128;
405: }
406: buffer.setCharAt(i, (char) cov_2char[c]);
407: }
408:
409: }
410:
411: return buffer.toString();
412: }
413:
414: private static int[] des_set_key(byte key[]) {
415: int schedule[] = new int[32];
416: int c = fourBytesToInt(key, 0);
417: int d = fourBytesToInt(key, 4);
418: int results[] = new int[2];
419: PERM_OP(d, c, 4, 0xf0f0f0f, results);
420: d = results[0];
421: c = results[1];
422: c = HPERM_OP(c, -2, 0xcccc0000);
423: d = HPERM_OP(d, -2, 0xcccc0000);
424: PERM_OP(d, c, 1, 0x55555555, results);
425: d = results[0];
426: c = results[1];
427: PERM_OP(c, d, 8, 0xff00ff, results);
428: c = results[0];
429: d = results[1];
430: PERM_OP(d, c, 1, 0x55555555, results);
431: d = results[0];
432: c = results[1];
433: d = (d & 0xff) << 16 | d & 0xff00 | (d & 0xff0000) >>> 16
434: | (c & 0xf0000000) >>> 4;
435: c &= 0xfffffff;
436: int j = 0;
437: for (int i = 0; i < 16; i++) {
438: if (shifts2[i]) {
439: c = c >>> 2 | c << 26;
440: d = d >>> 2 | d << 26;
441: } else {
442: c = c >>> 1 | c << 27;
443: d = d >>> 1 | d << 27;
444: }
445: c &= 0xfffffff;
446: d &= 0xfffffff;
447: int s = skb[0][c & 0x3f]
448: | skb[1][c >>> 6 & 0x3 | c >>> 7 & 0x3c]
449: | skb[2][c >>> 13 & 0xf | c >>> 14 & 0x30]
450: | skb[3][c >>> 20 & 0x1 | c >>> 21 & 0x6 | c >>> 22
451: & 0x38];
452: int t = skb[4][d & 0x3f]
453: | skb[5][d >>> 7 & 0x3 | d >>> 8 & 0x3c]
454: | skb[6][d >>> 15 & 0x3f]
455: | skb[7][d >>> 21 & 0xf | d >>> 22 & 0x30];
456: schedule[j++] = (t << 16 | s & 0xffff) & 0xffffffff;
457: s = s >>> 16 | t & 0xffff0000;
458: s = s << 4 | s >>> 28;
459: schedule[j++] = s & 0xffffffff;
460: }
461:
462: return schedule;
463: }
464:
465: private static int fourBytesToInt(byte b[], int offset) {
466: int value = byteToUnsigned(b[offset++]);
467: value |= byteToUnsigned(b[offset++]) << 8;
468: value |= byteToUnsigned(b[offset++]) << 16;
469: value |= byteToUnsigned(b[offset++]) << 24;
470: return value;
471: }
472:
473: private static final void intToFourBytes(int iValue, byte b[],
474: int offset) {
475: b[offset++] = (byte) (iValue & 0xff);
476: b[offset++] = (byte) (iValue >>> 8 & 0xff);
477: b[offset++] = (byte) (iValue >>> 16 & 0xff);
478: b[offset++] = (byte) (iValue >>> 24 & 0xff);
479: }
480:
481: public static final boolean matches(String encryptedPassword,
482: String enteredPassword) {
483: String salt = encryptedPassword.substring(0, 3);
484: String newCrypt = crypt(salt, enteredPassword);
485: return newCrypt.equals(encryptedPassword);
486: }
487:
488: }
|