/**
* Copyright 2009 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.safehaus.penrose.samba;
import org.safehaus.penrose.util.BinaryUtil;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import java.security.MessageDigest;
import java.math.BigInteger;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
/**
* @author Endi Sukma Dewata
*/
public class SambaUtil {
public static Logger log = LoggerFactory.getLogger(SambaUtil.class);
public static String encryptNTPassword(String password) throws Exception {
if (password == null) return null;
byte[] bytes = password.getBytes("UTF-16LE");
MessageDigest md = MessageDigest.getInstance("MD4");
md.update(bytes);
byte[] results = md.digest();
return BinaryUtil.encode(BinaryUtil.BIG_INTEGER, results).toUpperCase();
}
public static String encryptLMPassword(String password) throws Exception {
if (password == null) return null;
byte[] bytes = password.toUpperCase().getBytes("UTF-8");
if (bytes.length != 14) {
byte[] b = new byte[14];
for (int i=0; i<bytes.length && i<14; i++) b[i] = bytes[i];
bytes = b;
}
byte[] key1 = convert(bytes, 0);
byte[] key2 = convert(bytes, 7);
byte[] magic = new BigInteger("4B47532140232425", 16).toByteArray();
SecretKeySpec spec1 = new SecretKeySpec(key1, "DES");
Cipher cipher1 = Cipher.getInstance("DES");
cipher1.init(Cipher.ENCRYPT_MODE, spec1);
byte[] result1 = cipher1.doFinal(magic);
SecretKeySpec spec2 = new SecretKeySpec(key2, "DES");
Cipher cipher2 = Cipher.getInstance("DES");
cipher2.init(Cipher.ENCRYPT_MODE, spec2);
byte[] result2 = cipher2.doFinal(magic);
System.arraycopy(result2, 0, result1, 8, 8);
return BinaryUtil.encode(BinaryUtil.BIG_INTEGER, result1).toUpperCase();
}
public static byte[] convert(byte[] str, int i) {
byte[] key = new byte[8];
key[0] = (byte)(str[i] >> 1);
key[1] = (byte)(( ( str[i ] & 0x01 ) << 6 ) | ( str[i+1] >> 2 ));
key[2] = (byte)(( ( str[i+1] & 0x03 ) << 5 ) | ( str[i+2] >> 3 ));
key[3] = (byte)(( ( str[i+2] & 0x07 ) << 4 ) | ( str[i+3] >> 4 ));
key[4] = (byte)(( ( str[i+3] & 0x0F ) << 3 ) | ( str[i+4] >> 5 ));
key[5] = (byte)(( ( str[i+4] & 0x1F ) << 2 ) | ( str[i+5] >> 6 ));
key[6] = (byte)(( ( str[i+5] & 0x3F ) << 1 ) | ( str[i+6] >> 7 ));
key[7] = (byte)(str[i+6] & 0x7F);
for (int j=0; j<8; j++) key[j] = (byte)( key[j] << 1 );
return key;
}
}