CryptoSys PKI examples VB6 to VB.NET

HASH_File

Creates a cryptographic hash digest in byte format from a file.

VB6/VBA

Debug.Print "Testing HASH_File ..."
Dim nRet As Long
Dim abDigest() As Byte
Dim sFileName As String

' File to be hashed contains a total of 13 bytes: "hello world" plus CR-LF
' 68 65 6c 6c 6f 20 77 6f 72 6c 64 0d 0a   hello world..

sFileName = "hello.txt"

' Pre-dimension digest array - do this each time
ReDim abDigest(PKI_MAX_HASH_BYTES)

' Create default hash (SHA1) in binary mode
nRet = HASH_File(abDigest(0), PKI_MAX_HASH_BYTES, sFileName, 0)
If nRet > 0 Then ReDim Preserve abDigest(nRet - 1)
Debug.Print nRet, cnvHexStrFromBytes(abDigest)

' Use SHA1 in "text" mode
ReDim abDigest(PKI_MAX_HASH_BYTES)
nRet = HASH_File(abDigest(0), PKI_MAX_HASH_BYTES, sFileName, PKI_HASH_MODE_TEXT)
If nRet > 0 Then ReDim Preserve abDigest(nRet - 1)
Debug.Print nRet, cnvHexStrFromBytes(abDigest)

' Use MD5
ReDim abDigest(PKI_MAX_HASH_BYTES)
nRet = HASH_File(abDigest(0), PKI_MAX_HASH_BYTES, sFileName, PKI_HASH_MD5)
If nRet > 0 Then ReDim Preserve abDigest(nRet - 1)
Debug.Print nRet, cnvHexStrFromBytes(abDigest)

' Use MD5 in "text" mode
ReDim abDigest(PKI_MAX_HASH_BYTES)
nRet = HASH_File(abDigest(0), PKI_MAX_HASH_BYTES, sFileName, PKI_HASH_MD5 Or PKI_HASH_MODE_TEXT)
If nRet > 0 Then ReDim Preserve abDigest(nRet - 1)
Debug.Print nRet, cnvHexStrFromBytes(abDigest)

Output

Testing HASH_File ...
 20           88A5B867C3D110207786E66523CD1E4A484DA697
 20           22596363B3DE40B06F981FB85D82312E8C0ED511
 16           A0F2A3C1DCD5B1CAC71BF0C03F2FF1BD
 16           6F5902AC237024BDD0C176CB93063DC4

VB.NET

Console.WriteLine("Testing HASH_File ...")
''Dim nRet As Integer
Dim abDigest() As Byte
Dim sFileName As String

' File to be hashed contains a total of 13 bytes: "hello world" plus CR-LF
' 68 65 6c 6c 6f 20 77 6f 72 6c 64 0d 0a   hello world..

sFileName = "hello.txt"

' Create default hash (SHA1) in binary mode
abDigest = Hash.BytesFromFile(sFileName, HashAlgorithm.Sha1)
Console.WriteLine(abDigest.Length & " " & Cnv.ToHex(abDigest))

' Use SHA1 in "text" mode [FUDGE]
abDigest = Cnv.FromHex(Hash.HexFromTextFile(sFileName, HashAlgorithm.Sha1))
Console.WriteLine(abDigest.Length & " " & Cnv.ToHex(abDigest))

' Use MD5
abDigest = Hash.BytesFromFile(sFileName, HashAlgorithm.Md5)
Console.WriteLine(abDigest.Length & " " & Cnv.ToHex(abDigest))

' Use MD5 in "text" mode
abDigest = Cnv.FromHex(Hash.HexFromTextFile(sFileName, HashAlgorithm.Md5))
Console.WriteLine(abDigest.Length & " " & Cnv.ToHex(abDigest))

[Contents]

[HOME]   [NEXT: HASH_HexFromBytes...]

Copyright © 2010-20 D.I. Management Services Pty Ltd. All rights reserved.