Does anyone have any idea how to initialize (create/access) .NET objects in VBA that require initialization (arguments in the constructor) or static classes? Argument-less constructors can be (more or less) easily initialized, but I had no success initializing classes with arguments, or accessing functions in static classes.
Wanted to use System.IO.File to open a FileStream to be used from System.Security.Cryptography.SHA512Managed Currently I'm feeding the SHA512Managed from byte array, but there is a limitation on the size, also reading the whole file is time consuming.
Current code:
Private Function ReadFileBinary(FileName As String) As Byte()
Dim f As Long
f = FreeFile
Open FileName For Binary As f
ReDim ReadFileBinary(LOF(f) - 1)
Get f, , ReadFileBinary
Close f
End Function
Public Function FileHashSHA256(FileName As String) As String
Dim binfile() As Byte
On Error Resume Next
binfile = ReadFileBinary(FileName)
If Err Then
FileHashSHA256 = "FILE ERROR"
Err.Clear
Exit Function
End If
Set SHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")
FileHashSHA256 = ToHexString(SHA256.ComputeHash_2(binfile))
If Err Then
FileHashSHA256 = "HASH ERROR"
Err.Clear
End If
Set SHA256 = Nothing
End Function
Function ToHexString(rabyt)
With CreateObject("MSXML2.DOMDocument")
.LoadXML "<root />"
.DocumentElement.DataType = "bin.Hex"
.DocumentElement.nodeTypedValue = rabyt
ToHexString = Replace(.DocumentElement.text, vbLf, "")
End With
End Function
I know this could be done differently (e.g. with PowerShell), but the requirement is to do this with PS and .NET compiler disabled.