-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
'getGeneratedId( 「FOOBAR」)子(0,10)' - 很簡單的。當然,碰撞的可能性會增加。 –
當您沒有創建「UUID」時,討論'UUID'也是誤導。 – Kayaman
而且您不應該使用Java序列化將字符串轉換爲字節數組。只需使用string.getBytes(StandardCharsets.UTF8)。 –