CAPICOM - Accessing CryptoAPI from VB6

· Read in about 1 min · (196 words) ·

I’ve this need to calculate a file fingerprint in VB6. So the need to access CrptoAPI in vb6. Didnt want to do win32 calls from VB and thankfully MS has a COM layer over cryptoAPI called CAPICOM. Useful stuff…​as the MSDN site says

"Summary: CAPICOM is a new security technology from Microsoft that allows Microsoft Visual Basic, Visual Basic Script, ASP, and C++ programmers to easily incorporate digital signing and encryption into their application. (5 printed pages)"

Okay, now you can calculate SHA1, SHA-256 and SHA512 using VB. Trouble was that you cannot use the Filesystem object as it doesnt read binary files.

Here’s a small sample that does a SHA1 for binary files in VB6.

Const BUFFER_SIZE AsString= 4096
Dim sha As CAPICOM.HashedData
Set sha =New CAPICOM.HashedData

f =FreeFile
Open ""For Binary As f

Dim totalbytes AsLong
totalbytes =LOF(f)
Dim currentPos AsLong
currentPos = 0

While currentPos + BUFFER_SIZE <= totalbytes
Get f, , buffer
currentPos = currentPos + BUFFER_SIZE
sha.Hash buffer

Dim chunk() AsByte
Dim chunkSize AsLong
chunkSize = totalbytes - currentPos
If (chunkSize > 0) Then
ReDim chunk(1 To chunkSize)
Get f, , chunk
sha.Hash chunk

Close f
txtHash.Text = sha.Value