Subversion Repositories XServices

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 113
Line 1... Line -...
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;
1
/*     */ 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
/**
2
/*     */ 
37
 * A simple class for encoding and decoding passwords for CVS pserver protocol.
-
 
38
 * Can be used to recover forgotten passwords.
3
/*     */ import java.io.PrintStream;
39
 * 
-
 
40
 * <p>
4
/*     */ 
41
 * Adapted from: http://blog.zmeeagain.com/2005/01/recover-cvs-pserver-passwords.html
-
 
42
 */
-
 
43
public class CvsPassword
5
/*     */ public class CvsPassword
44
{
6
/*     */ {
45
    private static final char[] LOOKUP_TABLE = 
7
/*  46 */   private static final char[] LOOKUP_TABLE = { 
46
           {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8
/*  47 */     '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000', 'r', 'x', '5', 
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,
9
/*  48 */     'O', '\000', 'm', 'H', 'l', 'F', '@', 'L', 'C', 't', 'J', 'D', 'W', 'o', '4', 'K', 
49
            119, 49, 34, 82, 81, 95, 65, 112, 86, 118, 110, 122, 105, 41, 57,
10
/*  49 */     'w', '1', '"', 'R', 'Q', '_', 'A', 'p', 'V', 'v', 'n', 'z', 'i', ')', '9', 
50
            83, 43, 46, 102, 40, 89, 38, 103, 45, 50, 42, 123, 91, 35, 125, 55,
11
/*  50 */     'S', '+', '.', 'f', '(', 'Y', '&', 'g', '-', '2', '*', '{', '[', '#', '}', '7', 
51
            54, 66, 124, 126, 59, 47, 92, 71, 115, 78, 88, 107, 106, 56, 0,
12
/*  51 */     '6', 'B', '|', '~', ';', '/', '\\', 'G', 's', 'N', 'X', 'k', 'j', '8', 
52
            121, 117, 104, 101, 100, 69, 73, 99, 63, 94, 93, 39, 37, 61, 48,
13
/*  52 */     '\000', 'y', 'u', 'h', 'e', 'd', 'E', 'I', 'c', '?', '^', ']', '\'', '%', '=', '0', 
53
            58, 113, 32, 90, 44, 98, 60, 51, 33, 97, 62, 77, 84, 80, 85};
14
/*  53 */     ':', 'q', ' ', 'Z', ',', 'b', '<', '3', '!', 'a', '>', 'M', 'T', 'P', 'U' };
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
     */
15
/*     */ 
65
    public static String encode(String clearText)
16
/*     */   public static String encode(String clearText)
66
    {
17
/*     */   {
67
        // First character of encoded version is A:
-
 
68
        char[] encoded = new char[clearText.length() + 1];
18
/*  68 */     char[] encoded = new char[clearText.length() + 1];
69
        encoded[0] = 'A';
19
/*  69 */     encoded[0] = 'A';
70
        
-
 
71
        // Skip the first character:
20
/*     */ 
72
        int counter = 1;
21
/*  72 */     int counter = 1;
73
        for (char c : clearText.toCharArray())
22
/*  73 */     for (char c : clearText.toCharArray())
74
        {
23
/*     */     {
75
            if (c == '`' || c == '$' || c < 32)
24
/*  75 */       if ((c == '`') || (c == '$') || (c < ' '))
76
            {
25
/*     */       {
77
                throw new IllegalArgumentException(
26
/*  77 */         throw new IllegalArgumentException(
78
                        "Illegal character was found in clear password.");
27
/*  78 */           "Illegal character was found in clear password.");
79
            }
28
/*     */       }
80
            
29
/*     */ 
81
            encoded[counter++] = LOOKUP_TABLE[c];
30
/*  81 */       encoded[(counter++)] = LOOKUP_TABLE[c];
82
        }
31
/*     */     }
83
 
-
 
84
        return String.valueOf(encoded);
-
 
85
    }
-
 
86
 
-
 
87
    /**
-
 
88
     * Recovers an encoded via pserver protocol CVS password.
-
 
89
     * 
32
/*     */ 
90
     * @param encodedPassword
-
 
91
     *            the encoded password to be decoded
33
/*  84 */     return String.valueOf(encoded);
92
     *            
34
/*     */   }
93
     * @return the decoded password or null if the input was
-
 
94
     *      null or empty
-
 
95
     */
35
/*     */ 
96
    public static String decode(String encodedPassword)
36
/*     */   public static String decode(String encodedPassword)
97
    {
37
/*     */   {
98
        String rtn = null;
38
/*  98 */     String rtn = null;
99
        
39
/*     */ 
100
        if (encodedPassword != null && encodedPassword.length() > 0)
40
/* 100 */     if ((encodedPassword != null) && (encodedPassword.length() > 0))
101
        {
41
/*     */     {
102
            if (encodedPassword.startsWith("A"))
42
/* 102 */       if (encodedPassword.startsWith("A"))
103
            {
43
/*     */       {
104
                rtn = encode(encodedPassword.substring(1)).substring(1);
44
/* 104 */         rtn = encode(encodedPassword.substring(1)).substring(1);
105
            }
45
/*     */       }
106
            else
46
/*     */       else
107
            {
47
/*     */       {
108
                rtn = encode(encodedPassword).substring(1);
48
/* 108 */         rtn = encode(encodedPassword).substring(1);
109
            }
49
/*     */       }
110
        }
50
/*     */     }
111
        
51
/*     */ 
112
        return rtn;
52
/* 112 */     return rtn;
113
    }
53
/*     */   }
114
    
54
/*     */ 
115
    public static void main(String[] sArgs)
55
/*     */   public static void main(String[] sArgs)
116
    {
56
/*     */   {
117
        final String TEST_WORD = "i07w91";
57
/* 117 */     String TEST_WORD = "i07w91";
118
        String encoded = CvsPassword.encode(TEST_WORD);
58
/* 118 */     String encoded = encode("i07w91");
119
        System.out.println("Encoded: <" + encoded + ">");
59
/* 119 */     System.out.println("Encoded: <" + encoded + ">");
120
        String decoded = CvsPassword.decode(encoded);
60
/* 120 */     String decoded = decode(encoded);
121
        System.out.println("Decoded: <" + decoded + ">");
61
/* 121 */     System.out.println("Decoded: <" + decoded + ">");
122
        System.out.println(decoded.equals(TEST_WORD) ? "Test Passed" : "Test Failed");
62
/* 122 */     System.out.println(decoded.equals("i07w91") ? "Test Passed" : "Test Failed");
-
 
63
/*     */   }
123
    }
64
/*     */ }
124
}
65
 
-
 
66
/* Location:           C:\Users\brosenberger\Documents\My Box Files\XBridgeNG-download\XServices-20130131 - Kopie\WEB-INF\classes\net.zip
-
 
67
 * Qualified Name:     net.brutex.xservices.util.CvsPassword
-
 
68
 * JD-Core Version:    0.6.2
125
69
 */
126
70