Classes that implement
System.Security.Cryptography.ICryptoTransform are not thread safe.
This problem is caused by the way these classes are implemented using Microsoft CAPI/CNG patterns.
For example, when a hash class implements this interface, there would typically be an instance-specific hash object created (for example using
BCryptCreateHash function). This object can be called multiple times to add data to the hash (for example
BCryptHashData). Finally, a function is called that finishes the hash and returns the data (for example
Allowing the same hash object to be called with data from multiple threads before calling the finish function could potentially lead to incorrect results.
For example, if you have multiple threads hashing
"abc" on a static hash object, you may occasionally obtain the results (incorrectly) for hashing
"abcabc", or face other unexpected behavior.
It is very unlikely somebody outside Microsoft would write a class that implements
ICryptoTransform, and even if they do, it is likely that they will follow the same common pattern as the existing classes implementing this interface.
Any object that implements
System.Security.Cryptography.ICryptoTransform should not be used in concurrent threads as the instance members of such object are also not thread safe.
Potential problems may not be evident at first, but can range from explicit errors such as exceptions, to incorrect results when sharing an instance of such an object in multiple threads.
Create new instances of the object that implements or has a field of type
System.Security.Cryptography.ICryptoTransform to avoid sharing it accross multiple threads.
This example demonstrates the dangers of using a shared
System.Security.Cryptography.ICryptoTransform in a way that generates incorrect results or may raise an exception.
A simple fix is to change the local variable
sha1 being captured by the lambda to be a local variable within the lambda.
- Microsoft documentation, ThreadStaticAttribute Class.
- Stack Overflow, Why does SHA1.ComputeHash fail under high load with many threads?.
- Common Weakness Enumeration: CWE-362.