Share via

how to include system.security.cryptography in MS-Access VB

Anonymous
2015-12-18T07:57:12+00:00

I am looking to understand (1) how to include system.security.cryptography in MS-Access VB and (2) get my hands on a compact chunk of code to generate a hash code for a file; not a string as done by most of the examples.

Problem 1: From within MS-Access, I want to run an MD5 hashcode on a file to give me the 32 character hashcode.  It should be easy to get at the routines in system.security.cryptography, but all the examples use the VB "Imports" command.  I tried going to <Tools><References> and found System, but not System.Security.Cryptography.  What steps or code can I execute to access the MD5 hash code calls?

Problem 2: Many of the example MD5 algorithms shoe the the MD5 algorithm performed with input being a declared string; not a file stored in a folder.  Can someone please provide an example of how to run the hash code algorithm against a file?  I built one and it does not produce the same results as the off the shelf tools, so I know I am doing something wrong.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. Anonymous
    2015-12-24T04:12:17+00:00

    You know, just as a side note, I did actually see something like this once:

    Set hash = CreateObject("System.Security.Cryptography.MD5hash")

    It was in a forum, out in the wild, and I never tried it myself, but it appeared to be a working case, which lead me to believe that we may in fact be able to create native .NET types that are COM visible by default (which many are) through such syntax, without having to write a wrapper for it.

    I never did find any documentation on it (or look very hard for it), but I do specifically recall seeing that a System.Security.Crytpography class of some sort was instantiated through VBA's CreateObject().

    Ah, yes, a quick google search, here's an example of instantiating a .NET namespace directly through CreateObject:

    http://stackoverflow.com/questions/12036611/system-security-cryptography-hmacsha1-is-com-visible-how-should-i-call-computeh

    Might be easier.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2015-12-23T17:27:53+00:00

    I resolved the problems.

    (1) Found a packaged MD5 application on the internet that also provided source code.  I borrowed the 3 relevant functions and got the solution running in .NET.  Also downloaded 3 different MD5 applications from the internet and compared my result to the output of each of these applications.  Hurrah - they all matched!

    (2) The 2nd problem was that I had to make the .NET code available to my MS-Access application, which is a COM; not a .NET application.  I did this by putting a wrapper between the .NET code and the COM code.  I ended up using the Microsoft standard COM class - worked like a champ once I understood how to call it from the COM applicatin.  My thanks to  Tom van Stiphout for getting me started down the right path.  Tom - one suggested to improve your article... Don't just include the instructions on the .NET side.  Also provide sample code on the calling side.  Something like the following...

        '======================================

        ' the following MD5 hash / checksum calculation calls a COM-to-NET wrapper function

        '======================================

        ' create an object class

        Dim objMd5HashComPassThru As New ComClass1

        ' transcribe the path and filename into a string and output it to the immediate window

        ' so we can verify the content

        strFileWithFullPath = strEinFolder & strXmlFileName

        Debug.Print "Write_manifest step07 - file=" & strFileWithFullPath

        ' go calculate the MD5 checksum using a .NET function

        strMD5Hash = objMd5HashComPassThru.Md5HashComPassThru(strFileWithFullPath)

        Debug.Print "Write_manifest step08 - hash=" & strMD5Hash

        Dim nodChecksumAugmentationNum As IXMLDOMElement

        Set nodChecksumAugmentationNum = docXMLDOM.createElement("urn:ChecksumAugmentationNum")

        nodChecksumAugmentationNum.Text = strMD5Hash

        objManifestRootElement.lastChild.appendChild nodChecksumAugmentationNum.cloneNode(True)

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2015-12-24T03:59:23+00:00

    I will pass your suggestions on to the author of that article - not me.

    <conspiracy level="high">

    That code was probably posted by NSA.

    </conspiracy>

    Was this answer helpful?

    0 comments No comments
  4. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2015-12-18T13:38:53+00:00

    Access is COM-based, not .NET based. You will not be able to use .NET assemblies directly. However you can write such .NET component yourself and give it a COM wrapper.

    Here is one of many articles on how to write a .NET component and use it from a COM-based application like Access: http://www.dymeng.com/netcom/starters-guide/

    Was this answer helpful?

    0 comments No comments
  5. ScottGem 68,820 Reputation points Volunteer Moderator
    2015-12-18T13:05:43+00:00

    Not sure how much we can help. You are probably better off asking for help on this from the System.Security.Cryptography people. 

    While Access VBA is not the same as VB code meant to call an outside program may not be very different. Can you show us a VB code snippet you found? We may be able to suggest how to modify it.

    I'm not clear what you mean by "against a file". A Filename is a text string.

    Was this answer helpful?

    0 comments No comments