是否可以将String转换为NTLM哈希?我可以导入Java中的库,还是有一种方法可以用来获取它?
答案 0 :(得分:3)
在Java中实现的Type 3 NTLM响应计算位于appendix D的The NTLM Authentication Protocol and Security Support Provider。
答案 1 :(得分:1)
我写了这个实用程序类:
import jcifs.smb.NtlmPasswordAuthentication;
/**
* NTLM passwords encoding.
*
* This implementation depends on the JCIFS library.
*/
public class NTLMPassword {
private final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
private NTLMPassword() {
throw new UnsupportedOperationException("Can not instantiate this class.");
}
/**
* Return NTLM hash for a given string.
*
* See https://lists.samba.org/archive/jcifs/2015-February/010258.html
*
* @param value
* the string to hash.
* @return the NTLM hash for the given string.
*/
public static String encode(String value) {
String s = (value != null) ? value : "";
byte[] hash = NtlmPasswordAuthentication.nTOWFv1(s);
return bytesToHex(hash).toUpperCase();
}
/**
* See https://stackoverflow.com/a/9855338/1314986
*/
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
}
此代码使用JCIFS库。如果您使用Maven,请包含以下依赖项:
<dependency>
<groupId>org.codelibs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.18.2</version>
</dependency>
您可以使用以下测试验证此代码:
@Test
public void testEncode() throws Exception {
assertEquals("D36D0FC68CEDDAF7E180A6AE71096B35", NTLMPassword.encode("DummyPassword"));
}