1
/*****************************************************************************
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is the CVS Client Library.
27
* The Initial Developer of the Original Software is Robert Greig.
28
* Portions created by Robert Greig are Copyright (C) 2000.
29
* All Rights Reserved.
31
* If you wish your version of this file to be governed by only the CDDL
32
* or only the GPL Version 2, indicate your decision by adding
33
* "[Contributor] elects to include this software in this distribution
34
* under the [CDDL or GPL Version 2] license." If you do not indicate a
35
* single choice of license, a recipient has the option to distribute
36
* your version of this file under either the CDDL, the GPL Version 2 or
37
* to extend the choice of license to its licensees as provided above.
38
* However, if you add GPL Version 2 code and therefore, elected the GPL
39
* Version 2 license, then the option applies only if the new code is
40
* made subject to such option by the copyright holder.
42
* Contributor(s): Robert Greig.
43
*****************************************************************************/
44
package org.netbeans.modules.subversion.config;
46
import java.io.IOException;
47
import org.netbeans.modules.proxy.Base64Encoder;
50
* Scrambles text (the password) using the standard scheme described in the
51
* CVS protocol version 1.10. This encoding is trivial and should not be
52
* used for security, but rather as a mechanism for avoiding inadvertant
54
* @author Robert Greig, Tomas Stupka
56
public class Scrambler {
63
* The single instance of this class (Singleton design pattern)
65
public static Scrambler instance;
68
* Do not instantiate the scrambler directly. Use the getInstance() method
72
shifts = new int[256];
74
for (i = 0; i < 32; ++i) {
305
* Get an instance of the standard scrambler
307
public static Scrambler getInstance() {
308
if (instance == null) {
309
instance = new Scrambler();
315
* Scramble text, turning it into a String of scrambled data
316
* @return a String of scrambled data
318
public String scramble(String text) {
319
StringBuffer buf = new StringBuffer("A"); //NOI18N
322
for (int i = 0; i < text.length(); ++i) {
323
buf.append(scramble(text.charAt(i)));
326
return new String(encode(buf.toString().getBytes()));
329
public String descramble(String scrambledText) {
330
StringBuffer buf = new StringBuffer();
331
if (scrambledText != null) {
332
byte[] decoded = decode(scrambledText);
333
for (int i = 1; i < decoded.length; ++i) {
334
buf.append(scramble((char)decoded[i]));
337
return buf.toString();
340
private char scramble(char ch) {
341
int i = ( shifts[ (((int) ch) & 0xFF) & 255] & 255 ) &
342
( shifts[ (((int) ch) & 0x00FF) & 255] & 255 );
343
return Character.toChars(i)[0];
346
private byte[] decode(String str) {
347
return Base64Encoder.decode(str);
350
private byte[] encode(byte[] encode) {
351
return Base64Encoder.encode(encode).getBytes();