2016-12-29 37 views
0

是否可以將字符串轉換爲NTLM哈希?有沒有我可以導入的Java庫,或者是否有我可以用來獲取它的方法?是否有可能將字符串轉換爲Java中的NTLM哈希值?

+0

我想整個事情是:什麼類並表示NTML哈希值。我的意思是說:你可能正在談論這件事的一些具體實現;來自某個特定的圖書館。你不應該看看那個圖書館來確定如何在該圖書館中創建「NTML哈希」對象嗎?! – GhostCat

+2

我已經搜索了網頁的高低,我仍然不知道NTML是什麼。我發現每個搜索結果都是NTLM的拼寫錯誤。你的意思是NTLM嗎? – VGR

+0

@VGR yes i ment NTLM sorry – RACING121

回答

0

我寫了這個工具類:

import jcifs.smb.NtlmPasswordAuthentication; 

/** 
* NTLM passwords encoding. 
* 
* This implementation depends on the JCIFS library. 
*/ 
public class NTLMPassword { 

    private final static char[] HEX_ARRAY = "ABCDEF".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")); 
} 
+0

欣賞這更容易 – RACING121

相關問題