From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
01-13-2021 05:01 AM
Hello everyone,
I am trying to perform a SHA-256 hash of all files in a folder via LabVIEW.
I found this link: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha256managed?view=net-5.0
So I thought I would try to incorporate this in my LabVIEW code. I have no experience calling .dlls through LabVIEW or incorporating .NET in my VIs.
I could access the SHA256Managed Class by starting a Constructor Node in LabVIEW, selecting mscorlib(4.0.0.0): In my case this was under C:\Windows\Microsoft.NET\Framework\v2.0.50727
I thought now all I had to do is go back to the original link (from microsoft, linked above) and look at the properties, methods and etcetera to find out how to select the folder and which method would give me the hash.
I concluded (perhaps wrongly) that I would need to use ComputeHash (to get the results) with an inputStream which would reference to the folder I want to perform the SHA-256 hash on. However, I can't figure out how to pass on a reference to which folder I should select, because no matter what I do, I can't pass on a folder path to inputStream!
What am I doing wrong?
Any help would be greatly appreciated
Solved! Go to Solution.
01-13-2021 06:12 AM
If you have LabVIEW 2020, you can use the integrated File Checksum (File >> Adv File Funcs >> File Checksum.vi).
If not, use Read Binary File to read the content of each file and use the "buffer" overload for ComputeHash. (Or create a DotNet FileStream object from the path).
To get the hash of the folder, you could concatenate the individual hashes and use ComputeHash once more.
01-13-2021 08:39 AM - edited 01-13-2021 08:39 AM
Hi cord,
Thanks for the reply, I have done it for a single file, and when I convert my array containing the result to a string, I get a weird result:
This is my code
Any suggestions of what is wrong?
01-13-2021 08:43 AM - edited 01-13-2021 08:43 AM
The hash is binary, not text. Change the display mode for your string control to Hex!
01-13-2021 08:45 AM - edited 01-13-2021 08:45 AM
Also don't forget to Close the .Net refnum. Otherwise the object will stay in memory as long as your application is running. If you then executed this code multiple times, every time a new object will be created.
01-13-2021 08:47 AM
Thank you!
01-13-2021 08:52 AM
Ok, now the last weird thing is that when comparing the result of my hash vs some online hash tools, I get different results, but all online tools have the same results.
Any idea why?
01-13-2021 09:28 AM
Make sure you are reading the whole file, not just a single character 😉
And adding to what rolfk said, close the file reference from read binary file.