Create SHA256 COM Object in Access VBA - vba

I'm trying to create a SHA256 object from MS Access VBA.
I'm running Access 2016 on a Windows machine with .NET 4.8.
Public Function Base64_HMACSHA256(ByVal sTextToHash As String, ByVal sSharedSecretKey As String) As String
Dim asc As Object, enc As Object
Dim TextToHash() As Byte
Dim SharedSecretKey() As Byte
Set asc = CreateObject("System.Text.UTF8Encoding")
'Set enc = CreateObject("System.Security.Cryptography.HMACSHA256") 'THIS SUCCESSFULLY CREATES THE OBJECT
'Set enc = CreateObject("System.Security.Cryptography.SHA256") 'IHF 02/03/22 'CAN'T CREATE OBJECT
'Set enc = CreateObject("System.Security.Cryptography.SHA256CryptoServiceProvider") 'IHF 02/03/22 'CAN'T CREATE OBJECT
'Set enc = CreateObject("System.Security.Cryptography.RSACryptoServiceProvider") 'CAN'T CREATE OBJECT
TextToHash = asc.Getbytes_4(sTextToHash)
SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
enc.Key = SharedSecretKey
Dim bytes() As Byte
bytes = enc.ComputeHash_2((TextToHash))
Base64_HMACSHA256 = EncodeBase64(bytes)
Set asc = Nothing
Set enc = Nothing
End Function
I ended up doing it all a totally different way, so I never figured this out.

The appropriate class, as noted in the comments, is System.Security.Cryptography.SHA256Managed. Wikibooks has an example.
However, I prefer using the WinAPI CNG api directly (docs). This has some flexibility, performance and security advant.ages. See an example

Related

How do I compute HMACSHA256 in an Open Office VBasic Script?

Admittely I understand that I'm using a VBA script, but I cannot find information to confirm that this specific call in question is not specifically supported for OpenOffice. I currently have a ComputeHMACSha256 function that seems to be referenced several places online for computing Sha 256 hashes with a key string (albeit with Excel, NOT Open Office). However, when I attempt to run ComputeHash or the overload ComputeHash_2, I get this error:
I've got NET.2 and 3 installed and configured from what I can see in the Turn Windows feature screen:
And here is the code I'm using, taken from here:
Function CommandButton1_Click()
MsgBox("Hello World")
call UsageExample()
Call makeJWT()
End Function
Function makeJWT()
rsString = "{'alg': 'RS256','typ': 'JWT'}"
res = ComputeHMACSHA256("test", rsString)
MsgBox(res)
End Function
Function ComputeHMACSHA256(rsaKey As String, body As String) As String
Dim encoder As Object, crypto As Object
Dim hash() As Byte, hmacsha As String, i As Long
' compute HMACSHA256
Set encoder = CreateObject("System.Text.UTF8Encoding")
Set crypto = CreateObject("System.Security.Cryptography.HMACSHA256")
crypto.Key = encoder.GetBytes_4(rsaKey)
hash = crypto.ComputeHash_2(encoder.GetBytes_4(body))
' convert to an hexa string
hmacsha = String(64, "0")
For i = 0 To 31
Mid$(hmacsha, i + i + (hash(i) > 15) + 2) = Hex(hash(i))
Next
ComputeHMACSHA256 = LCase(hmacsha)
End Function

Import Class from DLL VBA

I have an (unregistered) DLL which I could access in VBA as follows:
Declare Function IBarcodeReader Lib "C:\Users\myname\Downloads\interop\zxing.dll" () As Long
Problem is, that this works for functions/subs, but I want to access a class contained in the DLL.
Background:
Trying to read QRCodes embedded in a pdf. Will convert PDF to Bitmap and then use the following library in VBA:
https://github.com/micjahn/ZXing.Net
Function Decode_QR_Code_From_File()
Dim reader As IBarcodeReader
Dim res As Result
Set reader = New BarcodeReader
reader.options.PossibleFormats.Add BarcodeFormat_QR_CODE
Set res = reader.DecodeImageFile("D:\Barcodes\QrCodes\www.png")
End Function
.
Function Decode_QR_Code_From_Byte_Array()
Dim reader As IBarcodeReader
Dim rawRGB(1000) As Byte
Dim res As Result
Set reader = New BarcodeReader
reader.options.PossibleFormats.Add BarcodeFormat_QR_CODE
Rem TODO: load bitmap data to byte array rawRGB
Set res = reader.DecodeImageBytes(rawRGB, 10, 10, BitmapFormat.BitmapFormat_Gray8)
End Function
.
Function Encode_To_QR_Code_To_File()
Dim writer As IBarcodeWriter
Dim qrCodeOptions As QrCodeEncodingOptions
Dim pixelDataResult As PixelData
Set qrCodeOptions = New QrCodeEncodingOptions
Set writer = New BarcodeWriter
writer.Format = BarcodeFormat_QR_CODE
Set writer.options = qrCodeOptions
qrCodeOptions.Height = 100
qrCodeOptions.Width = 100
qrCodeOptions.CharacterSet = "UTF-8"
qrCodeOptions.Margin = 10
qrCodeOptions.ErrorCorrection = ErrorCorrectionLevel_H
writer.WritePngToFile "Test", "D:\interop_qrcode.png"
Rem Or:
Set pixelDataResult = writer.Write("Test")
End Function
.
Function Decode_QR_Code_From_File_CreateObject()
Dim reader As IBarcodeReader
Dim res As Result
Set reader = CreateObject("ZXing.Interop.Decoding.BarcodeReader")
reader.options.PossibleFormats.Add BarcodeFormat_QR_CODE
Set res = reader.DecodeImageFile("D:\Barcodes\QrCodes\www.png")
End Function
Source: Using ZXing.Net with VBA
Edit:
re: registering DLL to be able to use Class methods
Have a seen/tried these suggestions:
How to register a C# or VB.Net DLL
"Successfully registered COM DLL, but can't use class methods"
VBA importing COM-registered dll and calling constructor
The question is unnecessary when ZXing.Net is registered correctly. Using ZXing.Net with VBA describes now in detail how to register ZXing.Net for use with VBA.

How do i unlabel a file using the TFS sdk and vb.net

I'm in the process of writing a little app for our SQL developers to allow them to create labels with TFS for easy code deployment, the trouble is the .ssmssqlproj files are being added to the label when ever i create one. I've added a sub to loop through and unlabel these file but i just will not work. code below
Public Sub UnlabelItem()
Dim returnValue As LabelResult()
Dim labelName As String = "1208-2210"
Dim labelScope As String = "$/"
Dim version As VersionSpec = New LabelVersionSpec(labelName, labelScope)
Dim path As String = "$/FEPI/Database/FEPI/000 Pre Tasks.ssmssqlproj"
Dim recursion As RecursionType = RecursionType.None
Dim itemspec As ItemSpec = New ItemSpec(path, recursion)
returnValue = sourceControl.UnlabelItem(labelName, labelScope, itemspec, version)
End Sub
this is a test Sub just to get it working and this is the error i get
Value of type 'Microsoft.TeamFoundation.VersionControl.Client.ItemSpec' cannot be converted to '1-dimensional array of Microsoft.TeamFoundation.VersionControl.Client.ItemSpec'
HAs anybody had any luck with the unlabel command?
Matt

System.Security.Cryptography.HMACSHA1 is Com visible. How should I call ComputeHash?

I want to create an HMAC SHA1 signature of some text in Microsoft Access
The .net object System.Security.Cryptography.HMACSHA1 is com visible, so I thought I could use that.
I can create the object and set the key, but when I come to call the compute function to get the hash, I am getting the error run time error 5 "Invalid Procedure Call or Argument"
Public Function sign(Key As String, Message As String)
Dim asc, enc
Dim MessageBytes() As bytes, HashBytes() As bytes
Set asc = CreateObject("System.Text.UTF8Encoding")
Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")
enc.Key = asc.GetBytes_4(Key)
MessageBytes = asc.GetBytes_4(Message)
HashBytes = enc.ComputeHash(MessageBytes)
sign = EncodeBase64(HashBytes)
Set asc = Nothing
Set enc = Nothing
End Function
I copied the Encoding bit from the web and I note there that GetBytes has transmuted into GetBytes_4. Is this name mangling? and do I need to do something similar to ComputeHash? and if so what (I tried _n where n = 1 to 6 to no avail).
If not What am I doing wrong?
I found the answer to my question in this question
Base64 HMAC SHA1 String in VBA
I thought I had tried enc.ComputeHash_2, but maybe I didn't because it seems to work

Encrypt in VB.NET With AES-256 and Decrypt in Air

I am trying to encrypt a zip in VB.NET to send to an android device using Air. Then once its on the device, decrypt it using the key and IV.
Here is part of my VB.NET Code:
Private Sub EncryptBytes(ByVal fileIn As String, ByVal fileOut As String, ByVal pass As String, ByVal ivString As String)
Dim crypt As New RijndaelManaged
crypt.KeySize = 256
crypt.BlockSize = 256
crypt.Padding = PaddingMode.PKCS7
crypt.Mode = CipherMode.CFB
'read byte array from file location, ie c:\temp\file.zip
Dim data As Byte() = ReadByteArray(fileIn)
Dim iv As Byte() = System.Text.Encoding.UTF8.GetBytes(ivString)
Dim key As Byte() = System.Text.Encoding.UTF8.GetBytes(pass)
Dim encryptor As ICryptoTransform = crypt.CreateEncryptor(key, iv)
Using ms As New System.IO.MemoryStream
Using cs As New CryptoStream(ms, encryptor, CryptoStreamMode.Write)
cs.Write(data, 0, data.Length)
cs.FlushFinalBlock()
End Using
'write byte array to file location, ie c:\temp\file_e.zip
WriteByteArray(fileOut, ms.ToArray)
End Using
End Sub
Two things, first I don't want to use PaddingMode.PKCS7 but when I change it to PaddingMode.None I get an error which says "Length of the data to encrypt is invalid" during decryption.
Second, I have a decryption SUB and it still works if I send it a bogus IV. Why is the IV not effecting the decryption process.
DECRYPTION
In Air I'm using the com.hurlant.crypto package I found at http://crypto.hurlant.com/docs/.
Here is my function:
public static function decryptZip(src:ByteArray, k:String, iv:String):ByteArray {
var key:ByteArray = Hex.toArray(Hex.fromString(k));
var mode:CFBMode = new CFBMode(new AESKey(key), new PKCS7(256));
mode.IV = Hex.toArray(Hex.fromString(iv));
mode.decrypt(src);
return src;
}
This is not working ... Note that I tried to write a PKCS7 class based on a Java class I found. The hurlant package I downloaded only had PKCS5. VB.NET did not provide any of the same padding classes as hurlant comes with. I wish I could using "None" but I couldn't get passed the error in VB.
I think there may also be an issue with the way I am converting a string into a byte array in VB vs Air.
Please help!
check out this sample code i am not sure if it's the solution you are looking for or not,
http://www.obviex.com/samples/Encryption.aspx