[jcifs] Code to initialize NTML password

jehan procaccia jehan.procaccia at it-sudparis.eu
Fri Oct 8 06:44:14 MDT 2010


  I finally found a code that compute a MD4 hash from jcifs library

FYI, here is a working code, if it can helps others ...

import java.io.UnsupportedEncodingException;
import jcifs.util.MD4;
import jcifs.util.Hexdump;

public class genereNT {
     public static void main(String[] args) {
         String password = "monpassword";

         String ntHash = "";
         MD4 md4 = new MD4();
         byte[] bpass;
         try {
             bpass = password.getBytes("UnicodeLittleUnmarked");

             md4.engineUpdate(bpass, 0, bpass.length);
             byte[] hashbytes = new byte[32];
             hashbytes = md4.engineDigest();
             ntHash = new String(Hexdump.toHexString(hashbytes, 0, 
hashbytes.length * 2));

             System.out.println(ntHash);
         }
         catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
     }
}

make it a function ("method" !) in an existing password modify servlet

...
newPassNT = genereNT(password);
...
public static String genereNT(String password) {

             String ntHash = "";
             MD4 md4 = new MD4();
             byte[] bpass = null;
             try {
                 bpass = password.getBytes("UnicodeLittleUnmarked");

                 md4.engineUpdate(bpass, 0, bpass.length);
                 byte[] hashbytes = new byte[32];
                 hashbytes = md4.engineDigest();
                 ntHash = new String(Hexdump.toHexString(hashbytes, 0, 
hashbytes.length * 2));

                 System.out.println("genreNT: "+ntHash + "bpass: "+bpass);
             }
             catch (UnsupportedEncodingException e) {
                 e.printStackTrace();
             }
             return ntHash;
         }
...
/* ldap replace */
         ModificationItem[] mods = new ModificationItem[2];
         mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, 
new BasicAttribute("userPassword", newPass));
         mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, 
new BasicAttribute("sambaNTPassword", newPassNT));

Le 03/10/2010 22:24, jehan procaccia a écrit :
>  Hi,
>
> I still can't find a way to compute ntml md4 hash for ldap 
> sambaNTpassword attribute .
> http://davenport.sourceforge.net/ntlm.html#appendixD gives a sample 
> java code to compute it (ntlmHash()) , but It always fails.
> apparently recent java.security.MessageDigest; don't know MD4 :-(, at 
> execution:
> => java.security.NoSuchAlgorithmException: MD4 MessageDigest not 
> available
> So I tried with gnu.crypto.hash.md4
> http://www.gnu.org/software/gnu-crypto/manual/api/gnu/crypto/hash/MD4.html 
>
> bu't I can't find a way to implement it, I'am quite newbie to java 
> programming ....
> Any help to compute from a clear text string a MD4 hash greatly 
> appreciated
>
> regards .
>
>
> Le 16/09/2010 21:20, Michael B Allen a écrit :
>> Hi Jehan,
>>
>> I think the sambaNTpassword is probably just the first hash
>> computation of the password without the challenge. It is a kookie DES
>> computation. I think it's the computation in the lmHash method in the
>> examples at the end of this page:
>>
>>    http://davenport.sourceforge.net/ntlm.html
>>
>> You definitely don't need JCIFS to calculate that hash but the
>> equivalent code in JCIFS is jcifs.smb.NtlmPasswordAuthentication.E
>> (although it uses a goofy DES class that should have been removed from
>> JCIFS long ago since I think you can do the same thing with Cipher des
>> = Cipher.getInstance("DES/ECB/NoPadding");).
>>
>> But I don't even know that that is what Samba is really putting in LDAP.
>>
>> Mike
>>
>> On Thu, Sep 16, 2010 at 12:08 PM, jehan procaccia
>> <jehan.procaccia at it-sudparis.eu>  wrote:
>>>   hello,
>>>
>>> I already have a java Servlet that sets unix userpassword field in 
>>> openldap.
>>> Now that I have added the samba.schema to my ldap server I want 
>>> people to be
>>> able to enter/initiate their sambaNTpassword  ( I understood that 
>>> there's no
>>> way to derive the NTpassword from the unix crypt password !?)
>>> So I am searching for a simple java code that computes a NTML (MD4 
>>> !?) hash
>>> from the clear text password string that my Servlet already gets 
>>> from the
>>> users input field .
>>>
>>> any sample code available ?
>>>
>>> Thanks .
>>>
>>
>>
>



More information about the jCIFS mailing list