01: package com.technoetic.xplanner.security.module.ntlm;
02:
03: import java.net.UnknownHostException;
04: import java.security.MessageDigest;
05: import java.security.SecureRandom;
06:
07: import jcifs.UniAddress;
08: import jcifs.smb.NtlmPasswordAuthentication;
09: import jcifs.smb.SmbException;
10: import jcifs.smb.SmbSession;
11: import org.apache.log4j.Priority;
12:
13: import com.technoetic.xplanner.security.util.Base64;
14:
15: public class NtlmLoginHelperImpl implements NtlmLoginHelper {
16: private final SecureRandom secureRandom = new SecureRandom();
17:
18: public void setLoggingPriority(Priority loggingPriority) {
19: }
20:
21: public void authenticate(String userId, String password,
22: String domainController, String domain)
23: throws UnknownHostException, SmbException {
24: UniAddress dc = UniAddress.getByName(domainController, true);
25:
26: NtlmPasswordAuthentication ntlm = new NtlmPasswordAuthentication(
27: domain, userId, password);
28:
29: SmbSession.logon(dc, ntlm);
30: }
31:
32: public String encodePassword(String password, byte[] salt)
33: throws Exception {
34: if (salt == null) {
35: salt = new byte[12];
36: secureRandom.nextBytes(salt);
37: }
38:
39: MessageDigest md = MessageDigest.getInstance("MD5");
40: md.update(salt);
41: md.update(password.getBytes("UTF8"));
42: byte[] digest = md.digest();
43: byte[] storedPassword = new byte[digest.length + 12];
44:
45: System.arraycopy(salt, 0, storedPassword, 0, 12);
46: System.arraycopy(digest, 0, storedPassword, 12, digest.length);
47:
48: return new String(Base64.encode(storedPassword));
49: }
50: }
|