message-digest

Need thread safe MessageDigest in Java

为君一笑 提交于 2019-11-29 21:28:53
I need to hash multiple keys from multiple threads using MessageDigest in a performance critical environment. I came to know that MessageDigest is not thread safe as it stores its state in it's object. What can be the best possible way to achieve thread safe hashing of keys? Use case: MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); //somewhere later, just need to hash a key, nothing else messageDigest.update(key); byte[] bytes = messageDigest.digest(); Specifically: Will ThreadLocal guaranteed to work? Will it have performance penalty? Are the objects returned by getInstance

Need thread safe MessageDigest in Java

半世苍凉 提交于 2019-11-28 19:05:09
问题 I need to hash multiple keys from multiple threads using MessageDigest in a performance critical environment. I came to know that MessageDigest is not thread safe as it stores its state in it's object. What can be the best possible way to achieve thread safe hashing of keys? Use case: MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); //somewhere later, just need to hash a key, nothing else messageDigest.update(key); byte[] bytes = messageDigest.digest(); Specifically: Will