Subversion Repositories XServices

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 brianR 1
/*
2
 *   Copyright 2012 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
17
package net.brutex.xservices.util;
18
/*
19
 * Copyright 2010 Andrew Kroh
20
 *
21
 * Licensed under the Apache License, Version 2.0 (the "License");
22
 * you may not use this file except in compliance with the License.
23
 * You may obtain a copy of the License at
24
 *
25
 * http://www.apache.org/licenses/LICENSE-2.0
26
 *
27
 * Unless required by applicable law or agreed to in writing, software
28
 * distributed under the License is distributed on an "AS IS" BASIS,
29
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30
 * See the License for the specific language governing permissions and
31
 * limitations under the License.
32
 */
33
 
34
 
35
 
36
/**
37
 * A simple class for encoding and decoding passwords for CVS pserver protocol.
38
 * Can be used to recover forgotten passwords.
39
 *
40
 * <p>
41
 * Adapted from: http://blog.zmeeagain.com/2005/01/recover-cvs-pserver-passwords.html
42
 */
43
public class CvsPassword
44
{
45
    private static final char[] LOOKUP_TABLE =
46
           {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 120, 53,
48
            79, 0, 109, 72, 108, 70, 64, 76, 67, 116, 74, 68, 87, 111, 52, 75,
49
            119, 49, 34, 82, 81, 95, 65, 112, 86, 118, 110, 122, 105, 41, 57,
50
            83, 43, 46, 102, 40, 89, 38, 103, 45, 50, 42, 123, 91, 35, 125, 55,
51
            54, 66, 124, 126, 59, 47, 92, 71, 115, 78, 88, 107, 106, 56, 0,
52
            121, 117, 104, 101, 100, 69, 73, 99, 63, 94, 93, 39, 37, 61, 48,
53
            58, 113, 32, 90, 44, 98, 60, 51, 33, 97, 62, 77, 84, 80, 85};
54
 
55
    /**
56
     * Encodes a CVS password to be used in .cvspass file. Throws an exception
57
     * if clearText is null, if a character is found outside the 0 - 126 range, or
58
     * if within the range, one of the non-allowed characters.
59
     *
60
     * @param clearText
61
     *            the password in clear to be encoded
62
     *
63
     * @return the encoded cvs password
64
     */
65
    public static String encode(String clearText)
66
    {
67
        // First character of encoded version is A:
68
        char[] encoded = new char[clearText.length() + 1];
69
        encoded[0] = 'A';
70
 
71
        // Skip the first character:
72
        int counter = 1;
73
        for (char c : clearText.toCharArray())
74
        {
75
            if (c == '`' || c == '$' || c < 32)
76
            {
77
                throw new IllegalArgumentException(
78
                        "Illegal character was found in clear password.");
79
            }
80
 
81
            encoded[counter++] = LOOKUP_TABLE[c];
82
        }
83
 
84
        return String.valueOf(encoded);
85
    }
86
 
87
    /**
88
     * Recovers an encoded via pserver protocol CVS password.
89
     *
90
     * @param encodedPassword
91
     *            the encoded password to be decoded
92
     *
93
     * @return the decoded password or null if the input was
94
     *      null or empty
95
     */
96
    public static String decode(String encodedPassword)
97
    {
98
        String rtn = null;
99
 
100
        if (encodedPassword != null && encodedPassword.length() > 0)
101
        {
102
            if (encodedPassword.startsWith("A"))
103
            {
104
                rtn = encode(encodedPassword.substring(1)).substring(1);
105
            }
106
            else
107
            {
108
                rtn = encode(encodedPassword).substring(1);
109
            }
110
        }
111
 
112
        return rtn;
113
    }
114
 
115
    public static void main(String[] sArgs)
116
    {
117
        final String TEST_WORD = "i07w91";
118
        String encoded = CvsPassword.encode(TEST_WORD);
119
        System.out.println("Encoded: <" + encoded + ">");
120
        String decoded = CvsPassword.decode(encoded);
121
        System.out.println("Decoded: <" + decoded + ">");
122
        System.out.println(decoded.equals(TEST_WORD) ? "Test Passed" : "Test Failed");
123
    }
124
}
125