]> git.argeo.org Git - lgpl/argeo-commons.git/blob - PasswordSandbox.java
de9a1eb1e6a1c4dc7c23c0fc09e1d9501d8cd338
[lgpl/argeo-commons.git] / PasswordSandbox.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.security;
17
18 import org.apache.commons.codec.DecoderException;
19 import org.apache.commons.codec.binary.Base64;
20 import org.apache.commons.codec.binary.Hex;
21 import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder;
22
23 public class PasswordSandbox {
24 public static void main(String[] args) {
25 try {
26 // Tested password
27 String pwdPlain = "demo";
28
29 // Check Java generated values
30 LdapShaPasswordEncoder lspe = new LdapShaPasswordEncoder();
31 String pwdLdapShaBase64 = lspe.encodePassword(pwdPlain, null);
32 System.out.println("pwdLdapShaBase64:\t\t" + pwdLdapShaBase64);
33
34 String pwdShaBase64 = pwdLdapShaBase64.substring("{SHA}".length());
35 System.out.println("pwdShaBase64:\t\t\t" + pwdShaBase64);
36
37 byte[] pwdShaArray = Base64.decodeBase64(pwdShaBase64.getBytes());
38 String pwdShaHex = new String(Hex.encodeHex(pwdShaArray));
39 System.out.println("pwdShaHex:\t\t\t" + pwdShaHex);
40
41 // Check that we can use JavaScript generated values in Hex
42 String jsShaHex = "89e495e7941cf9e40e6980d14a16bf023ccd4c91";
43 System.out.println("jsShaHex:\t\t\t" + pwdShaHex);
44 System.out.println("pwdShaHex==jsShaHex:\t\t"
45 + (pwdShaHex.equals(jsShaHex)));
46
47 byte[] jsShaArray = Hex.decodeHex(jsShaHex.toCharArray());
48 String jsShaBase64 = new String(Base64.encodeBase64(jsShaArray));
49 System.out.println("jsShaBase64:\t\t\t" + jsShaBase64);
50 System.out.println("pwdShaBase64==jsShaBase64:\t"
51 + (pwdShaBase64.equals(jsShaBase64)));
52 } catch (DecoderException e) {
53 e.printStackTrace();
54 }
55
56 }
57
58 }