001: /*****************************************************************************
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025:
026: * The Original Software is the CVS Client Library.
027: * The Initial Developer of the Original Software is Robert Greig.
028: * Portions created by Robert Greig are Copyright (C) 2000.
029: * All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041:
042: * Contributor(s): Robert Greig.
043: *****************************************************************************/package org.netbeans.modules.subversion.config;
044:
045: import java.io.IOException;
046: import org.netbeans.modules.proxy.Base64Encoder;
047:
048: /**
049: * Scrambles text (the password) using the standard scheme described in the
050: * CVS protocol version 1.10. This encoding is trivial and should not be
051: * used for security, but rather as a mechanism for avoiding inadvertant
052: * compromise.
053: * @author Robert Greig, Tomas Stupka
054: */
055: public class Scrambler {
056: /**
057: * The mapping array
058: */
059: private int[] shifts;
060:
061: /**
062: * The single instance of this class (Singleton design pattern)
063: */
064: public static Scrambler instance;
065:
066: /**
067: * Do not instantiate the scrambler directly. Use the getInstance() method
068: */
069: private Scrambler() {
070: int i;
071: shifts = new int[256];
072:
073: for (i = 0; i < 32; ++i) {
074: shifts[i] = i;
075: }
076:
077: shifts[i++] = 114;
078: shifts[i++] = 120;
079: shifts[i++] = 53;
080: shifts[i++] = 79;
081: shifts[i++] = 96;
082: shifts[i++] = 109;
083: shifts[i++] = 72;
084: shifts[i++] = 108;
085: shifts[i++] = 70;
086: shifts[i++] = 64;
087: shifts[i++] = 76;
088: shifts[i++] = 67;
089: shifts[i++] = 116;
090: shifts[i++] = 74;
091: shifts[i++] = 68;
092: shifts[i++] = 87;
093: shifts[i++] = 111;
094: shifts[i++] = 52;
095: shifts[i++] = 75;
096: shifts[i++] = 119;
097: shifts[i++] = 49;
098: shifts[i++] = 34;
099: shifts[i++] = 82;
100: shifts[i++] = 81;
101: shifts[i++] = 95;
102: shifts[i++] = 65;
103: shifts[i++] = 112;
104: shifts[i++] = 86;
105: shifts[i++] = 118;
106: shifts[i++] = 110;
107: shifts[i++] = 122;
108: shifts[i++] = 105;
109: shifts[i++] = 41;
110: shifts[i++] = 57;
111: shifts[i++] = 83;
112: shifts[i++] = 43;
113: shifts[i++] = 46;
114: shifts[i++] = 102;
115: shifts[i++] = 40;
116: shifts[i++] = 89;
117: shifts[i++] = 38;
118: shifts[i++] = 103;
119: shifts[i++] = 45;
120: shifts[i++] = 50;
121: shifts[i++] = 42;
122: shifts[i++] = 123;
123: shifts[i++] = 91;
124: shifts[i++] = 35;
125: shifts[i++] = 125;
126: shifts[i++] = 55;
127: shifts[i++] = 54;
128: shifts[i++] = 66;
129: shifts[i++] = 124;
130: shifts[i++] = 126;
131: shifts[i++] = 59;
132: shifts[i++] = 47;
133: shifts[i++] = 92;
134: shifts[i++] = 71;
135: shifts[i++] = 115;
136: shifts[i++] = 78;
137: shifts[i++] = 88;
138: shifts[i++] = 107;
139: shifts[i++] = 106;
140: shifts[i++] = 56;
141: shifts[i++] = 36;
142: shifts[i++] = 121;
143: shifts[i++] = 117;
144: shifts[i++] = 104;
145: shifts[i++] = 101;
146: shifts[i++] = 100;
147: shifts[i++] = 69;
148: shifts[i++] = 73;
149: shifts[i++] = 99;
150: shifts[i++] = 63;
151: shifts[i++] = 94;
152: shifts[i++] = 93;
153: shifts[i++] = 39;
154: shifts[i++] = 37;
155: shifts[i++] = 61;
156: shifts[i++] = 48;
157: shifts[i++] = 58;
158: shifts[i++] = 113;
159: shifts[i++] = 32;
160: shifts[i++] = 90;
161: shifts[i++] = 44;
162: shifts[i++] = 98;
163: shifts[i++] = 60;
164: shifts[i++] = 51;
165: shifts[i++] = 33;
166: shifts[i++] = 97;
167: shifts[i++] = 62;
168: shifts[i++] = 77;
169: shifts[i++] = 84;
170: shifts[i++] = 80;
171: shifts[i++] = 85;
172: shifts[i++] = 223;
173: shifts[i++] = 225;
174: shifts[i++] = 216;
175: shifts[i++] = 187;
176: shifts[i++] = 166;
177: shifts[i++] = 229;
178: shifts[i++] = 189;
179: shifts[i++] = 222;
180: shifts[i++] = 188;
181: shifts[i++] = 141;
182: shifts[i++] = 249;
183: shifts[i++] = 148;
184: shifts[i++] = 200;
185: shifts[i++] = 184;
186: shifts[i++] = 136;
187: shifts[i++] = 248;
188: shifts[i++] = 190;
189: shifts[i++] = 199;
190: shifts[i++] = 170;
191: shifts[i++] = 181;
192: shifts[i++] = 204;
193: shifts[i++] = 138;
194: shifts[i++] = 232;
195: shifts[i++] = 218;
196: shifts[i++] = 183;
197: shifts[i++] = 255;
198: shifts[i++] = 234;
199: shifts[i++] = 220;
200: shifts[i++] = 247;
201: shifts[i++] = 213;
202: shifts[i++] = 203;
203: shifts[i++] = 226;
204: shifts[i++] = 193;
205: shifts[i++] = 174;
206: shifts[i++] = 172;
207: shifts[i++] = 228;
208: shifts[i++] = 252;
209: shifts[i++] = 217;
210: shifts[i++] = 201;
211: shifts[i++] = 131;
212: shifts[i++] = 230;
213: shifts[i++] = 197;
214: shifts[i++] = 211;
215: shifts[i++] = 145;
216: shifts[i++] = 238;
217: shifts[i++] = 161;
218: shifts[i++] = 179;
219: shifts[i++] = 160;
220: shifts[i++] = 212;
221: shifts[i++] = 207;
222: shifts[i++] = 221;
223: shifts[i++] = 254;
224: shifts[i++] = 173;
225: shifts[i++] = 202;
226: shifts[i++] = 146;
227: shifts[i++] = 224;
228: shifts[i++] = 151;
229: shifts[i++] = 140;
230: shifts[i++] = 196;
231: shifts[i++] = 205;
232: shifts[i++] = 130;
233: shifts[i++] = 135;
234: shifts[i++] = 133;
235: shifts[i++] = 143;
236: shifts[i++] = 246;
237: shifts[i++] = 192;
238: shifts[i++] = 159;
239: shifts[i++] = 244;
240: shifts[i++] = 239;
241: shifts[i++] = 185;
242: shifts[i++] = 168;
243: shifts[i++] = 215;
244: shifts[i++] = 144;
245: shifts[i++] = 139;
246: shifts[i++] = 165;
247: shifts[i++] = 180;
248: shifts[i++] = 157;
249: shifts[i++] = 147;
250: shifts[i++] = 186;
251: shifts[i++] = 214;
252: shifts[i++] = 176;
253: shifts[i++] = 227;
254: shifts[i++] = 231;
255: shifts[i++] = 219;
256: shifts[i++] = 169;
257: shifts[i++] = 175;
258: shifts[i++] = 156;
259: shifts[i++] = 206;
260: shifts[i++] = 198;
261: shifts[i++] = 129;
262: shifts[i++] = 164;
263: shifts[i++] = 150;
264: shifts[i++] = 210;
265: shifts[i++] = 154;
266: shifts[i++] = 177;
267: shifts[i++] = 134;
268: shifts[i++] = 127;
269: shifts[i++] = 182;
270: shifts[i++] = 128;
271: shifts[i++] = 158;
272: shifts[i++] = 208;
273: shifts[i++] = 162;
274: shifts[i++] = 132;
275: shifts[i++] = 167;
276: shifts[i++] = 209;
277: shifts[i++] = 149;
278: shifts[i++] = 241;
279: shifts[i++] = 153;
280: shifts[i++] = 251;
281: shifts[i++] = 237;
282: shifts[i++] = 236;
283: shifts[i++] = 171;
284: shifts[i++] = 195;
285: shifts[i++] = 243;
286: shifts[i++] = 233;
287: shifts[i++] = 253;
288: shifts[i++] = 240;
289: shifts[i++] = 194;
290: shifts[i++] = 250;
291: shifts[i++] = 191;
292: shifts[i++] = 155;
293: shifts[i++] = 142;
294: shifts[i++] = 137;
295: shifts[i++] = 245;
296: shifts[i++] = 235;
297: shifts[i++] = 163;
298: shifts[i++] = 242;
299: shifts[i++] = 178;
300: shifts[i++] = 152;
301: }
302:
303: /**
304: * Get an instance of the standard scrambler
305: */
306: public static Scrambler getInstance() {
307: if (instance == null) {
308: instance = new Scrambler();
309: }
310: return instance;
311: }
312:
313: /**
314: * Scramble text, turning it into a String of scrambled data
315: * @return a String of scrambled data
316: */
317: public String scramble(String text) {
318: StringBuffer buf = new StringBuffer("A"); //NOI18N
319:
320: if (text != null) {
321: for (int i = 0; i < text.length(); ++i) {
322: buf.append(scramble(text.charAt(i)));
323: }
324: }
325: return new String(encode(buf.toString().getBytes()));
326: }
327:
328: public String descramble(String scrambledText) {
329: StringBuffer buf = new StringBuffer();
330: if (scrambledText != null) {
331: byte[] decoded = decode(scrambledText);
332: for (int i = 1; i < decoded.length; ++i) {
333: buf.append(scramble((char) decoded[i]));
334: }
335: }
336: return buf.toString();
337: }
338:
339: private char scramble(char ch) {
340: int i = (shifts[(((int) ch) & 0xFF) & 255] & 255)
341: & (shifts[(((int) ch) & 0x00FF) & 255] & 255);
342: return Character.toChars(i)[0];
343: }
344:
345: private byte[] decode(String str) {
346: return Base64Encoder.decode(str);
347: }
348:
349: private byte[] encode(byte[] encode) {
350: return Base64Encoder.encode(encode).getBytes();
351: }
352: }
|