/** * Creates the LM Hash of the user's password. * * @param password The password. * @return The LM Hash of the given password */ public static String lmHash(final String password) throws Exception { final byte[] oemPassword = password.toUpperCase().getBytes("US-ASCII"); final int length = Math.min(oemPassword.length, 14); final byte[] keyBytes = new byte[14]; System.arraycopy(oemPassword, 0, keyBytes, 0, length); final Key lowKey = SMBHash.createDESKey(keyBytes, 0); final Key highKey = SMBHash.createDESKey(keyBytes, 7); final byte[] magicConstant = "KGS!@#$%".getBytes("US-ASCII"); final Cipher des = Cipher.getInstance("DES/ECB/NoPadding"); des.init(Cipher.ENCRYPT_MODE, lowKey); final byte[] lowHash = des.doFinal(magicConstant); des.init(Cipher.ENCRYPT_MODE, highKey); final byte[] highHash = des.doFinal(magicConstant); final byte[] lmHash = new byte[16]; System.arraycopy(lowHash, 0, lmHash, 0, 8); System.arraycopy(highHash, 0, lmHash, 8, 8); return SMBHash.toHexString(lmHash); }
/** * Creates the NTLM Hash of the user's password. * * @param password The password. * @return The NTLM Hash of the given password. */ public static String ntlmHash(final String password) throws Exception { final byte[] unicodePassword = password.getBytes("UnicodeLittleUnmarked"); final MessageDigest md4 = MessageDigest.getInstance("MD4"); return SMBHash.toHexString(md4.digest(unicodePassword)); }