Attribute VB_Name = "basCRC32File"
Option Explicit
Option Base 0

' basCRC32File: Calculates CRC-32 checksum for a file.
' Calls crc32 function in basCRC32.
' Version 1. Published 23 October 2006.
' Updated 17 June 2020 - changed license on this code to MIT.

' ******************************* LICENSE ***********************************
' * Copyright (C) 2006-20 David Ireland, DI Management Services Pty Limited.
' * All rights reserved. <www.di-mgt.com.au> <www.cryptosys.net>
' * The code in this module is licensed under the terms of the MIT license.  
' * For a copy, see <http://opensource.org/licenses/MIT>
' ***************************************************************************

' Please forward comments or bug reports to https://di-mgt.com.au/contact

Public Function CRC32Sum(sFileName As String) As String
' Computes the CRC-32 checksum for a file
    Dim sContents As String
    Dim lCRC As Long
    Dim sPad As String
    
    sContents = ReadFileIntoString(sFileName)
    If Len(sContents) = 0 Then
        Exit Function
    End If
    
    lCRC = crc32(sContents)
    CRC32Sum = Hex(lCRC)
    
    ' Pad to exactly 8 hex digits
    If Len(CRC32Sum) <> 8 Then
        sPad = String(8 - Len(CRC32Sum), "0")
        CRC32Sum = sPad & CRC32Sum
    End If
    
End Function

Private Function ReadFileIntoString(sFilePath As String) As String
' Reads file (if it exists) into a string.
    Dim strIn As String
    Dim hFile As Integer
    
    ' Check if file exists
    If Len(Dir(sFilePath)) = 0 Then
        Exit Function
    End If
    hFile = FreeFile
    Open sFilePath For Binary Access Read As #hFile
    strIn = Input(LOF(hFile), #hFile)
    Close #hFile
    ReadFileIntoString = strIn
    
End Function