2017-07-01 24 views
-1

我怎樣才能減少sh1哈希到少於10位數的長度?可能嗎 ?是否可以將sh1令牌的長度限制爲小於10?

我試圖用下面的代碼

/** 
* 
* @param id 
* @return 
*/ 
public static final String getGeneratedId(final String id) { 

    try { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     ObjectOutputStream oos = new ObjectOutputStream(baos); 
     oos.writeObject(id); 
     oos.close(); 
     MessageDigest m = MessageDigest.getInstance("SHA1"); 
     m.update(baos.toByteArray()); 
     return new BigInteger(1, m.digest()).toString(16); 
    } catch (Exception e) { 
     ICAMPPLogger.logException(CLASSNAME, "error in creating uuid" + e); 
     ICAMPPUtils.getStackTraceToString(e); 
     return BigInteger.ZERO.toString(); 
    } 
} 
public static void main(String[] args) { 
    String token = getGeneratedId("testing"); 
    System.out.println(token); 
} 
ouput is : 1305340859400297508806692338645894167742475232778 
But i would like to limit length to less than 10 . is it posssible 
+2

'getGeneratedId( 「FOOBAR」)子(0,10)' - 很簡單的。當然,碰撞的可能性會增加。 –

+0

當您沒有創建「UUID」時,討論'UUID'也是誤導。 – Kayaman

+1

而且您不應該使用Java序列化將字符串轉換爲字節數組。只需使用string.getBytes(StandardCharsets.UTF8)。 –

回答

-1

SHA1產生數據的160個比特,這意味着20字節。如果您將其表示爲HEX,則需要40個字符。

它的全部在編碼。您可能能夠找到一些編碼,這將需要少於40個字符。

檢查:。What is the most efficient binary to text encoding?

相關問題