1
1
/**
2
2
* Copyright (C) 2000 - 2009 Silverpeas
3
3
*
4
- * This program is free software: you can redistribute it and/or modify
5
- * it under the terms of the GNU Affero General Public License as
6
- * published by the Free Software Foundation, either version 3 of the
7
- * License, or (at your option) any later version.
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the
5
+ * GNU Affero General Public License as published by the Free Software Foundation, either version 3
6
+ * of the License, or (at your option) any later version.
8
7
*
9
- * As a special exception to the terms and conditions of version 3.0 of
10
- * the GPL, you may redistribute this Program in connection with Free/Libre
11
- * Open Source Software ("FLOSS") applications as described in Silverpeas's
12
- * FLOSS exception. You should have received a copy of the text describing
13
- * the FLOSS exception, and it is also available here:
8
+ * As a special exception to the terms and conditions of version 3.0 of the GPL, you may
9
+ * redistribute this Program in connection with Free/Libre Open Source Software ("FLOSS")
10
+ * applications as described in Silverpeas's FLOSS exception. You should have received a copy of the
11
+ * text describing the FLOSS exception, and it is also available here:
14
12
* "http://www.silverpeas.com/legal/licensing"
15
13
*
16
- * This program is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU Affero General Public License for more details.
14
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
+ * Affero General Public License for more details.
20
17
*
21
- * You should have received a copy of the GNU Affero General Public License
22
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ * You should have received a copy of the GNU Affero General Public License along with this program.
19
+ * If not, see <http://www.gnu.org/licenses/>.
23
20
*/
24
-
25
21
package com .silverpeas .openoffice .util ;
26
22
27
23
import java .io .UnsupportedEncodingException ;
43
39
*/
44
40
public class PasswordManager {
45
41
46
- private final static byte [] KEY = new byte [] { -23 , -75 , -2 , -17 , 79 , -94 , -125 ,
47
- -14 };
42
+ private final static byte [] KEY = new byte []{ -23 , -75 , -2 , -17 , 79 , -94 , -125 ,
43
+ -14 };
48
44
private final static Key decryptionKey = new SecretKeySpec (KEY , "DES" );
49
45
private final static String DIGITS = "0123456789abcdef" ;
50
46
51
47
/**
52
48
* Converts a hexadecimal String to clear password.
53
- * @param hex hexadecimal String to convert
49
+ *
50
+ * @param encodedPassword password to decode.
54
51
* @return resulting password
52
+ * @throws UnsupportedEncodingException
53
+ * @throws GeneralSecurityException
55
54
*/
56
- public static String decodePassword (String encodedPassword ) throws
57
- UnsupportedEncodingException , GeneralSecurityException {
55
+ public static char [] decodePassword (String encodedPassword ) throws UnsupportedEncodingException ,
56
+ GeneralSecurityException {
58
57
Cipher cipher = Cipher .getInstance ("DES" );
59
58
cipher .init (Cipher .DECRYPT_MODE , decryptionKey );
60
59
byte [] bytes = new BigInteger (encodedPassword , 16 ).toByteArray ();
61
- Logger .getLogger (Launcher .class .getName ()).log (Level .INFO , "decrypted password byte array length : {0}" , bytes .length );
60
+ Logger .getLogger (Launcher .class .getName ()).log (Level .INFO ,
61
+ "decrypted password byte array length : {0}" , bytes .length );
62
62
int nbCaracToRemove = (bytes .length ) % 8 ;
63
63
byte [] result = new byte [bytes .length - nbCaracToRemove ];
64
- System .arraycopy (bytes , nbCaracToRemove , result , 0 , bytes .length -
65
- nbCaracToRemove );
66
- return new String (cipher .doFinal (result ), "UTF-8" );
64
+ System .arraycopy (bytes , nbCaracToRemove , result , 0 , bytes .length - nbCaracToRemove );
65
+ return new String (cipher .doFinal (result ), "UTF-8" ).toCharArray ();
67
66
}
68
67
69
68
/**
70
69
* Converts a password to a hexadecimal String containing the DES encrypted password.
70
+ *
71
71
* @param password the password to encrypt
72
72
* @return resulting hexadecimal String
73
+ * @throws UnsupportedEncodingException
74
+ * @throws GeneralSecurityException
73
75
*/
74
- public static String encodePassword (String password ) throws
75
- UnsupportedEncodingException , GeneralSecurityException {
76
+ public static String encodePassword (String password ) throws UnsupportedEncodingException ,
77
+ GeneralSecurityException {
76
78
Cipher cipher = Cipher .getInstance ("DES" );
77
79
cipher .init (Cipher .ENCRYPT_MODE , decryptionKey );
78
80
byte [] cipherText = cipher .doFinal (password .getBytes ("UTF-8" ));
79
- StringBuilder buf = new StringBuilder ();
81
+ StringBuilder buf = new StringBuilder (cipherText . length );
80
82
for (int i = 0 ; i != cipherText .length ; i ++) {
81
83
int v = cipherText [i ] & 0xff ;
82
84
buf .append (DIGITS .charAt (v >> 4 ));
@@ -87,27 +89,30 @@ public static String encodePassword(String password) throws
87
89
88
90
/**
89
91
* Build an authentication objec from arguments
90
- * @param args arguments passed through JNLP
92
+ *
93
+ * @param login
94
+ * @param encodedPassword
91
95
* @return the Authentication object
92
96
*/
93
97
public static AuthenticationInfo extractAuthenticationInfo (String login , String encodedPassword ) {
94
- String clearPwd = null ;
95
- String decodedLogin = login ;
96
98
try {
97
- clearPwd = decodePassword (encodedPassword );
98
- if (clearPwd .isEmpty () ) {
99
+ char [] clearPwd = decodePassword (encodedPassword );
100
+ if (clearPwd .length <= 0 ) {
99
101
clearPwd = promptForpassword ();
100
102
}
101
- decodedLogin = URLDecoder .decode (login , "UTF-8" );
102
- } catch (Exception e ) {
103
- Logger .getLogger (Launcher .class .getName ()).log (Level .SEVERE , "can't retrieve credentials" , e );
103
+ String decodedLogin = URLDecoder .decode (login , "UTF-8" );
104
+ return new AuthenticationInfo (decodedLogin , clearPwd );
105
+ } catch (GeneralSecurityException ex ) {
106
+ Logger .getLogger (Launcher .class .getName ()).log (Level .SEVERE , "can't retrieve credentials" , ex );
107
+ System .exit (-1 );
108
+ } catch (UnsupportedEncodingException ex ) {
109
+ Logger .getLogger (Launcher .class .getName ()).log (Level .SEVERE , "can't retrieve credentials" , ex );
104
110
System .exit (-1 );
105
111
}
106
-
107
- return new AuthenticationInfo (decodedLogin , clearPwd );
112
+ return null ;
108
113
}
109
114
110
- private static String promptForpassword () {
115
+ private static char [] promptForpassword () {
111
116
return MessageDisplayer .displayPromptPassword ();
112
117
}
113
118
}
0 commit comments