// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.Security;

import javax.mail.internet.MimeUtility;

public class MD4 {
    public static void main(String[] args) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        System.out.println(md4_hex(md4_hex("password") + "user".toLowerCase()));
    }
    public static String md4_hex(String s) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD4");
        return toHex(md.digest(s.getBytes()));
    }
    private static String toHex(byte[] ba) {
        char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
                            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < ba.length; i++) {
           sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
           sb.append(hexdigit[ba[i] & 0x0F]);
        }
        return sb.toString();
     }
    public static String b64encode(byte[] b) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream b64os = MimeUtility.encode(baos, "base64");
        b64os.write(b);
        b64os.close();
        return new String(baos.toByteArray());
     }
}
