blob: 54d737413ecf9a24a3bde7969d6a3f4f2b8af27b [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21package org.apache.qpid.management.common.sasl;
22
23import org.apache.harmony.javax.security.auth.callback.Callback;
24import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
25import org.apache.harmony.javax.security.auth.callback.NameCallback;
26import org.apache.harmony.javax.security.auth.callback.PasswordCallback;
27import org.apache.harmony.javax.security.auth.callback.UnsupportedCallbackException;
28import java.io.IOException;
29import java.io.UnsupportedEncodingException;
30import java.security.MessageDigest;
31import java.security.NoSuchAlgorithmException;
32
33
34public class UsernameHashedPasswordCallbackHandler implements CallbackHandler
35{
36 private String user;
37 private char[] pwchars;
38
39 public UsernameHashedPasswordCallbackHandler(String user, String password) throws Exception
40 {
41 this.user = user;
42 this.pwchars = getHash(password);
43 }
44
45 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
46 {
47 for (int i = 0; i < callbacks.length; i++)
48 {
49 if (callbacks[i] instanceof NameCallback)
50 {
51 NameCallback ncb = (NameCallback) callbacks[i];
52 ncb.setName(user);
53 }
54 else if (callbacks[i] instanceof PasswordCallback)
55 {
56 PasswordCallback pcb = (PasswordCallback) callbacks[i];
57 pcb.setPassword(pwchars);
58 }
59 else
60 {
61 throw new UnsupportedCallbackException(callbacks[i]);
62 }
63 }
64 }
65
66
67 private void clearPassword()
68 {
69 if (pwchars != null)
70 {
71 for (int i = 0 ; i < pwchars.length ; i++)
72 {
73 pwchars[i] = 0;
74 }
75 pwchars = null;
76 }
77 }
78
79 protected void finalize()
80 {
81 clearPassword();
82 }
83
84 public static char[] getHash(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
85 {
86 byte[] data = text.getBytes("utf-8");
87
88 MessageDigest md = MessageDigest.getInstance("MD5");
89
90 for (byte b : data)
91 {
92 md.update(b);
93 }
94
95 byte[] digest = md.digest();
96
97 char[] hash = new char[digest.length ];
98
99 int index = 0;
100 for (byte b : digest)
101 {
102 hash[index++] = (char) b;
103 }
104
105 return hash;
106 }
107}