How to use .NET 3.5 class from Excel 2010 VBA

PhilSwEngr

New Member
Joined
Sep 18, 2013
Messages
8
Is there a way I can use System.Security.Cryptography from an Excel 2010 VBA module subroutine to calculate an MD5 checksum of a file? In C# I use the statement:

using System.Security.Cryptography;

MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();

Since System.Security.Cryptography is available as part of .NET 3.5, I s/b able to use it from Excel VBA w/o having to register any DLLs, correct?

Any help is appreciated.
 
This works for me:
Code:
Public Function HashFile(fpath As String) As String
    
    Dim byt() As Byte, x As Long
    
    With CreateObject("adodb.stream")
        .Open
        .Type = 1
        .LoadFromFile fpath
        byt = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider").ComputeHash_2(.Read)
        .Close
    End With
    
    For x = LBound(byt) To UBound(byt)
        HashFile = HashFile & IIf(Len(Hex(byt(x))) > 1, Hex(byt(x)), "0" & Hex(byt(x)))
    Next x
    
    
    HashFile = LCase(HashFile)
    
    
End Function
 
Upvote 0
Kyle, you are the man! The code you posted produces checksums identical to those produced by my C# implementation for both text and binary files. Thanks!

:)
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top