I assume that computing a md5 hash is a pure deterministic function, and hence I assume it should be safe to use a single C# MD5CryptoServiceProvider instance to compute hashed in multithreaded code, because I assume that the md5 computation would be implemented as a pure function.
I do not know the details of the implementation but I am surprised that it implements IDisposable, as I do not understand what kind of cleanup is required from something which I assume is implemented as a pure function.
So am I right in assuming that it is safe to call MD5CryptoServiceProvider.ComputeHash on a single instance of MD5CryptoServiceProvider in multithreaded code ?
CodePudding user response:
Your assumption is correct.
I do not know the details of the implementation but I am surprised that it implements IDisposable,
And why surprised? I would expect this method to rely on OS level cryptography functions that are implemented possibly as a COM interface and MAY be good to release when not in use any further.
It even says so: "using the implementation provided by the cryptographic service provider (CSP)."
Looking at the source (instead of wondering), which you find simply by using google an that points to https://referencesource.microsoft.com/#mscorlib/system/security/cryptography/md5cryptoserviceprovider.cs - there is a handle involved that is also disposed in that dispose call.
You seem to think that this is running native code - this may or may not be the case, and on windows is makes little sense to reimplement this when it is already in the OS.