6

I use MessageDigest to calculate the md5 signature in my project, but during the performance test it throws an ArrayIndexOutOfBoundsException.

I have found a few posts that suggest this is because MessageDigest is a singleton and not thread safe. Does anyone know how I can get around this problem, or if there is an equivalent MessageDigest class that is thread safe?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Vince.Wu
  • 870
  • 1
  • 10
  • 17

1 Answers1

19

somebody says that this is beacause MessageDigest is singleton

That would be your MessageDigest object. Not the class itself. MessageDigest.getInstance() always returns a new instance: see the Javadoc.

and not thread save.

Thread safe.

Now, anyone knows how to solve this problem

Don't share your MessageDigest instance among multiple threads. Don't even make it a class member, make it a local variable in the method(s) that call it.

user207421
  • 305,947
  • 44
  • 307
  • 483