KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > config > Scrambler


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.modules.subversion.config;
23
24 import java.io.IOException JavaDoc;
25
26 /**
27  * Scrambles text (the password) using the standard scheme described in the
28  * CVS protocol version 1.10. This encoding is trivial and should not be
29  * used for security, but rather as a mechanism for avoiding inadvertant
30  * compromise.
31  * @author Robert Greig, Tomas Stupka
32  */

33 public class Scrambler {
34     /**
35      * The mapping array
36      */

37     private int[] shifts;
38
39     /**
40      * The single instance of this class (Singleton design pattern)
41      */

42     public static Scrambler instance;
43
44     /**
45      * Do not instantiate the scrambler directly. Use the getInstance() method
46      */

47     private Scrambler() {
48         int i;
49         shifts = new int[256];
50
51         for (i = 0; i < 32; ++i) {
52             shifts[i] = i;
53         }
54
55         shifts[i++] = 114;
56         shifts[i++] = 120;
57         shifts[i++] = 53;
58         shifts[i++] = 79;
59         shifts[i++] = 96;
60         shifts[i++] = 109;
61         shifts[i++] = 72;
62         shifts[i++] = 108;
63         shifts[i++] = 70;
64         shifts[i++] = 64;
65         shifts[i++] = 76;
66         shifts[i++] = 67;
67         shifts[i++] = 116;
68         shifts[i++] = 74;
69         shifts[i++] = 68;
70         shifts[i++] = 87;
71         shifts[i++] = 111;
72         shifts[i++] = 52;
73         shifts[i++] = 75;
74         shifts[i++] = 119;
75         shifts[i++] = 49;
76         shifts[i++] = 34;
77         shifts[i++] = 82;
78         shifts[i++] = 81;
79         shifts[i++] = 95;
80         shifts[i++] = 65;
81         shifts[i++] = 112;
82         shifts[i++] = 86;
83         shifts[i++] = 118;
84         shifts[i++] = 110;
85         shifts[i++] = 122;
86         shifts[i++] = 105;
87         shifts[i++] = 41;
88         shifts[i++] = 57;
89         shifts[i++] = 83;
90         shifts[i++] = 43;
91         shifts[i++] = 46;
92         shifts[i++] = 102;
93         shifts[i++] = 40;
94         shifts[i++] = 89;
95         shifts[i++] = 38;
96         shifts[i++] = 103;
97         shifts[i++] = 45;
98         shifts[i++] = 50;
99         shifts[i++] = 42;
100         shifts[i++] = 123;
101         shifts[i++] = 91;
102         shifts[i++] = 35;
103         shifts[i++] = 125;
104         shifts[i++] = 55;
105         shifts[i++] = 54;
106         shifts[i++] = 66;
107         shifts[i++] = 124;
108         shifts[i++] = 126;
109         shifts[i++] = 59;
110         shifts[i++] = 47;
111         shifts[i++] = 92;
112         shifts[i++] = 71;
113         shifts[i++] = 115;
114         shifts[i++] = 78;
115         shifts[i++] = 88;
116         shifts[i++] = 107;
117         shifts[i++] = 106;
118         shifts[i++] = 56;
119         shifts[i++] = 36;
120         shifts[i++] = 121;
121         shifts[i++] = 117;
122         shifts[i++] = 104;
123         shifts[i++] = 101;
124         shifts[i++] = 100;
125         shifts[i++] = 69;
126         shifts[i++] = 73;
127         shifts[i++] = 99;
128         shifts[i++] = 63;
129         shifts[i++] = 94;
130         shifts[i++] = 93;
131         shifts[i++] = 39;
132         shifts[i++] = 37;
133         shifts[i++] = 61;
134         shifts[i++] = 48;
135         shifts[i++] = 58;
136         shifts[i++] = 113;
137         shifts[i++] = 32;
138         shifts[i++] = 90;
139         shifts[i++] = 44;
140         shifts[i++] = 98;
141         shifts[i++] = 60;
142         shifts[i++] = 51;
143         shifts[i++] = 33;
144         shifts[i++] = 97;
145         shifts[i++] = 62;
146         shifts[i++] = 77;
147         shifts[i++] = 84;
148         shifts[i++] = 80;
149         shifts[i++] = 85;
150         shifts[i++] = 223;
151         shifts[i++] = 225;
152         shifts[i++] = 216;
153         shifts[i++] = 187;
154         shifts[i++] = 166;
155         shifts[i++] = 229;
156         shifts[i++] = 189;
157         shifts[i++] = 222;
158         shifts[i++] = 188;
159         shifts[i++] = 141;
160         shifts[i++] = 249;
161         shifts[i++] = 148;
162         shifts[i++] = 200;
163         shifts[i++] = 184;
164         shifts[i++] = 136;
165         shifts[i++] = 248;
166         shifts[i++] = 190;
167         shifts[i++] = 199;
168         shifts[i++] = 170;
169         shifts[i++] = 181;
170         shifts[i++] = 204;
171         shifts[i++] = 138;
172         shifts[i++] = 232;
173         shifts[i++] = 218;
174         shifts[i++] = 183;
175         shifts[i++] = 255;
176         shifts[i++] = 234;
177         shifts[i++] = 220;
178         shifts[i++] = 247;
179         shifts[i++] = 213;
180         shifts[i++] = 203;
181         shifts[i++] = 226;
182         shifts[i++] = 193;
183         shifts[i++] = 174;
184         shifts[i++] = 172;
185         shifts[i++] = 228;
186         shifts[i++] = 252;
187         shifts[i++] = 217;
188         shifts[i++] = 201;
189         shifts[i++] = 131;
190         shifts[i++] = 230;
191         shifts[i++] = 197;
192         shifts[i++] = 211;
193         shifts[i++] = 145;
194         shifts[i++] = 238;
195         shifts[i++] = 161;
196         shifts[i++] = 179;
197         shifts[i++] = 160;
198         shifts[i++] = 212;
199         shifts[i++] = 207;
200         shifts[i++] = 221;
201         shifts[i++] = 254;
202         shifts[i++] = 173;
203         shifts[i++] = 202;
204         shifts[i++] = 146;
205         shifts[i++] = 224;
206         shifts[i++] = 151;
207         shifts[i++] = 140;
208         shifts[i++] = 196;
209         shifts[i++] = 205;
210         shifts[i++] = 130;
211         shifts[i++] = 135;
212         shifts[i++] = 133;
213         shifts[i++] = 143;
214         shifts[i++] = 246;
215         shifts[i++] = 192;
216         shifts[i++] = 159;
217         shifts[i++] = 244;
218         shifts[i++] = 239;
219         shifts[i++] = 185;
220         shifts[i++] = 168;
221         shifts[i++] = 215;
222         shifts[i++] = 144;
223         shifts[i++] = 139;
224         shifts[i++] = 165;
225         shifts[i++] = 180;
226         shifts[i++] = 157;
227         shifts[i++] = 147;
228         shifts[i++] = 186;
229         shifts[i++] = 214;
230         shifts[i++] = 176;
231         shifts[i++] = 227;
232         shifts[i++] = 231;
233         shifts[i++] = 219;
234         shifts[i++] = 169;
235         shifts[i++] = 175;
236         shifts[i++] = 156;
237         shifts[i++] = 206;
238         shifts[i++] = 198;
239         shifts[i++] = 129;
240         shifts[i++] = 164;
241         shifts[i++] = 150;
242         shifts[i++] = 210;
243         shifts[i++] = 154;
244         shifts[i++] = 177;
245         shifts[i++] = 134;
246         shifts[i++] = 127;
247         shifts[i++] = 182;
248         shifts[i++] = 128;
249         shifts[i++] = 158;
250         shifts[i++] = 208;
251         shifts[i++] = 162;
252         shifts[i++] = 132;
253         shifts[i++] = 167;
254         shifts[i++] = 209;
255         shifts[i++] = 149;
256         shifts[i++] = 241;
257         shifts[i++] = 153;
258         shifts[i++] = 251;
259         shifts[i++] = 237;
260         shifts[i++] = 236;
261         shifts[i++] = 171;
262         shifts[i++] = 195;
263         shifts[i++] = 243;
264         shifts[i++] = 233;
265         shifts[i++] = 253;
266         shifts[i++] = 240;
267         shifts[i++] = 194;
268         shifts[i++] = 250;
269         shifts[i++] = 191;
270         shifts[i++] = 155;
271         shifts[i++] = 142;
272         shifts[i++] = 137;
273         shifts[i++] = 245;
274         shifts[i++] = 235;
275         shifts[i++] = 163;
276         shifts[i++] = 242;
277         shifts[i++] = 178;
278         shifts[i++] = 152;
279     }
280
281     /**
282      * Get an instance of the standard scrambler
283      */

284     public static Scrambler getInstance() {
285         if (instance == null) {
286             instance = new Scrambler();
287         }
288         return instance;
289     }
290
291     /**
292      * Scramble text, turning it into a String of scrambled data
293      * @return a String of scrambled data
294      */

295     public String JavaDoc scramble(String JavaDoc text) {
296         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("A"); //NOI18N
297

298         if (text != null) {
299             for (int i = 0; i < text.length(); ++i) {
300                 buf.append(scramble(text.charAt(i)));
301             }
302         }
303         return new String JavaDoc(encode(buf.toString().getBytes()));
304     }
305     
306     public String JavaDoc descramble(String JavaDoc scrambledText) {
307         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
308         if (scrambledText != null) {
309             byte[] decoded = decode(scrambledText);
310             for (int i = 1; i < decoded.length; ++i) {
311                 buf.append(scramble((char)decoded[i]));
312             }
313         }
314         return buf.toString();
315     }
316     
317     private char scramble(char ch) {
318         byte newCh = (byte)(shifts[(int)ch & 255] & 255);
319         return (char) newCh;
320     }
321
322     private byte[] decode(String JavaDoc str) {
323         try {
324             return new sun.misc.BASE64Decoder().decodeBuffer(str);
325         }
326         catch (IOException JavaDoc ex) {
327             
328         }
329         return null;
330     }
331     
332     private byte[] encode(byte[] encode) {
333         return new sun.misc.BASE64Encoder().encode(encode).getBytes();
334     }
335 }
336
Popular Tags