• Vui lòng đọc nội qui diễn đàn để tránh bị xóa bài viết
  • Tìm kiếm trước khi đặt câu hỏi

Tính mã MD5 cho tệp tin

Các bài viết hướng dẫn, giúp các bạn hiểu và tiếp cận với Visual Basic nhanh hơn
User avatar
tuyen_dt18
Guru
Guru
Posts: 261
Joined: Sat 19/04/2008 8:46 pm
Location: Hưng Yên
Been thanked: 43 times
Contact:

Tính mã MD5 cho tệp tin

Postby tuyen_dt18 » Sat 03/08/2013 1:30 am

Tên bài viết: Tính mã MD5 cho tệp tin
Tác giả: tuyen_dt18
Cấp độ bài viết:
Tóm tắt: Tính mã MD5 cho tệp tin bằng ngôn ngữ VB6



1) Các bạn chạy VB6.EXE vào tạo 1 Project mới
2) Project gồm có :
+ 1 Form : Tên tùy ý
: Trên Form gồm có : + 2 TextBox có tên lần lượt là Text1, Text2
+ 1 Command Button với tên mặc định "Command1"
+ 1 Class : Tên cMD5

3) Chép code :
+ Chép code sau đây và dán vào phần code của Form:
  1. Private WithEvents MD5 As cMD5
  2.  
  3. Private Sub Command1_Click()
  4.     Set MD5 = New cMD5
  5.     Text2.Text = MD5.CalculateMD5sum(Text1.Text)
  6. End Sub
  7.  
  8. Private Sub MD5_Finish(ByVal bSuccess As Boolean)
  9.     Command1.Enabled = True
  10. End Sub
  11.  
  12. Private Sub MD5_InProgress(ByVal Percent As Integer)
  13.     Me.Caption = Percent & " %"
  14. End Sub
  15.  
  16. Private Sub MD5_Start()
  17.     Command1.Enabled = False
  18. End Sub
  19.  



+ Chép code sau và dán vào phần code của class :

[vb]Option Explicit

Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextW" (ByVal phProv As Long, ByVal pszContainer As Long, ByVal pszProvider As Long, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, ByVal phHash As Long) As Long
Private Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal hHash As Long) As Long
Private Declare Function CryptHashData Lib "advapi32.dll" (ByVal hHash As Long, ByVal pbData As Long, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptGetHashParam Lib "advapi32.dll" (ByVal hHash As Long, ByVal dwParam As Long, ByVal pByte As Long, ByVal pdwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwFlags As Long) As Long
Private Declare Function GetLastError Lib "kernel32.dll" () As Long
Private Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal nNumberOfBytesToRead As Long, ByVal lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function GetFileSize Lib "kernel32.dll" (ByVal hFile As Long, ByVal lpFileSizeHigh As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function VirtualAlloc Lib "kernel32.dll" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFree Lib "kernel32.dll" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long

Private Const MS_ENHANCED_PROV As String = "Microsoft Enhanced Cryptographic Provider v1.0"
Private Const PROV_RSA_FULL As Long = 1
Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000
Private Const NTE_EXISTS As Long = &H8009000F
Private Const ALG_CLASS_HASH As Long = (4 * 2 ^ 13)
Private Const ALG_TYPE_ANY As Long = 0
Private Const ALG_SID_MD5 As Long = 3
Private Const CALG_MD5 As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5)
Private Const HP_HASHVAL As Long = &H2
Private Const GENERIC_READ As Long = &H80000000
Private Const FILE_SHARE_READ As Long = &H1
Private Const OPEN_EXISTING As Long = 3
Private Const INVALID_HANDLE_VALUE As Long = (-1)
Private Const INVALID_FILE_SIZE As Long = &HFFFFFFFF
Private Const MEM_RESERVE As Long = &H2000
Private Const MEM_COMMIT As Long = &H1000
Private Const MEM_RELEASE As Long = &H8000
Private Const PAGE_READWRITE As Long = &H4

Private Type MD5Context
digest(0 To 15) As Byte
hHash As Long
End Type

Public Event Start()
Public Event InProgress(ByVal Percent As Integer)
Public Event Finish(ByVal bSuccess As Boolean)

Public Function CalculateMD5sum(ByVal strFileName As String) As String
Const BufferSize As Long = &H200000 '2 MB

Dim hFile As Long
Dim lngRead As Long
Dim lngFileSize As Long
Dim lngFileSizeHi As Long
Dim md5Hash As MD5Context
Dim hCryptProv As Long
Dim lpBuffer As Long
Dim lngCalculated As Long
Dim lngRemaining As Long
Dim lngBufferSize As Long

RaiseEvent Start

lngBufferSize = BufferSize
'
hFile = CreateFile(ByVal StrPtr(strFileName), GENERIC_READ, FILE_SHARE_READ, ByVal 0, OPEN_EXISTING, 0, ByVal 0)
If (hFile = INVALID_HANDLE_VALUE) Then
CalculateMD5sum = ""
RaiseEvent Finish(False)
Exit Function
End If
'
lngFileSize = GetFileSize(hFile, ByVal VarPtr(lngFileSizeHi))
If ((lngFileSize = INVALID_FILE_SIZE) Or (lngFileSize = 0)) Then
Call CloseHandle(hFile)
CalculateMD5sum = ""
RaiseEvent Finish(False)
Exit Function
End If
'
If (CryptStartup(hCryptProv) = False) Then
Call CloseHandle(hFile)
CalculateMD5sum = ""
RaiseEvent Finish(False)
Exit Function
End If
'
lpBuffer = VirtualAlloc(ByVal 0, lngBufferSize, MEM_COMMIT Or MEM_RESERVE, PAGE_READWRITE)
If (lpBuffer = 0) Then
Call CloseHandle(hFile)
CalculateMD5sum = ""
RaiseEvent Finish(False)
Exit Function
End If
'
Call MD5Init(md5Hash, hCryptProv)

If (lngFileSize <= BufferSize) Then
lngBufferSize = lngFileSize
Call ReadFile(hFile, lpBuffer, lngBufferSize, ByVal VarPtr(lngRead), ByVal 0)
If (lngRead <> lngBufferSize) Then
Call VirtualFree(lpBuffer, ByVal 0, MEM_RELEASE)
Call CloseHandle(hFile)
Call CryptCleanup(hCryptProv)
CalculateMD5sum = ""
RaiseEvent Finish(False)
Exit Function
End If
'
Call MD5Update(md5Hash, lpBuffer, lngRead)
RaiseEvent InProgress(CInt(CSng(lngRead) / CSng(lngFileSize) * CSng(100)))
Else
lngRemaining = (lngFileSize - lngCalculated)
While (lngRemaining > 0)
If (lngRemaining < BufferSize) Then
lngBufferSize = lngRemaining
Else
lngBufferSize = BufferSize
End If
'
DoEvents
Call ReadFile(hFile, lpBuffer, lngBufferSize, ByVal VarPtr(lngRead), ByVal 0)
If (lngRead <> lngBufferSize) Then
Call VirtualFree(lpBuffer, ByVal 0, MEM_RELEASE)
Call CloseHandle(hFile)
Call CryptCleanup(hCryptProv)
CalculateMD5sum = ""
RaiseEvent Finish(False)
Exit Function
End If
'
Call MD5Update(md5Hash, lpBuffer, lngRead)
lngCalculated = lngCalculated + lngRead
lngRemaining = (lngFileSize - lngCalculated)
'
RaiseEvent InProgress(CInt(CSng(lngCalculated) / CSng(lngFileSize) * CSng(100)))
Wend
End If
'
Call MD5Final(md5Hash)
'

Dim I As Integer
Dim B As Byte
Dim H As String
'
For I = 0 To 15
B = md5Hash.digest(I)
H = Hex$(B)
If (Len(H) < 2) Then
CalculateMD5sum = CalculateMD5sum & "0"
End If
CalculateMD5sum = CalculateMD5sum & H
Next I
'
Call CryptCleanup(hCryptProv)
Call VirtualFree(lpBuffer, ByVal 0, MEM_RELEASE)
Call CloseHandle(hFile)
'
RaiseEvent Finish(True)
End Function
'
Private Function CryptStartup(ByRef m_hCryptProv As Long) As Boolean
If (CryptAcquireContext(ByVal VarPtr(m_hCryptProv), ByVal 0, ByVal StrPtr(MS_ENHANCED_PROV), PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) = 0) Then
If (GetLastError() = NTE_EXISTS) Then
If (CryptAcquireContext(ByVal VarPtr(m_hCryptProv), ByVal 0, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) = 0) Then
CryptStartup = False
Exit Function
End If
Else
CryptStartup = False
Exit Function
End If

End If
CryptStartup = True
End Function

Private Sub CryptCleanup(ByRef m_hCryptProv As Long)
If (m_hCryptProv <> 0) Then
Call CryptReleaseContext(m_hCryptProv, 0)
End If
m_hCryptProv = 0
End Sub

Private Sub MD5Init(ByRef Ctx As MD5Context, ByVal m_hCryptProv As Long)
Call CryptCreateHash(m_hCryptProv, CALG_MD5, 0, 0, ByVal VarPtr(Ctx.hHash))
End Sub


Private Sub MD5Update(ByRef Ctx As MD5Context, ByVal lpBuffer As Long, ByVal dwLength As Long)
Call CryptHashData(ByVal Ctx.hHash, ByVal lpBuffer, dwLength, 0)
End Sub

Private Sub MD5Final(ByRef Ctx As MD5Context)
Dim dwCount As Long
dwCount = 16
Call CryptGetHashParam(ByVal Ctx.hHash, HP_HASHVAL, ByVal VarPtr(Ctx.digest(0)), ByVal VarPtr(dwCount), 0)
If (Ctx.hHash <> 0) Then
Call CryptDestroyHash(ByVal Ctx.hHash)
End If
Ctx.hHash = 0
End Sub

[/vb]


4) Nhấn F5 để thử chương trình !

5) Khi chương trình đã chạy, nhập đường dẫn của một tệp tin trên máy tính của bạn vào Text1. Sau đó nhấn nút Calculate để xem mã MD5 của tệp tin đó.


Hỏi ý kiến tác giả trước khi sử dụng hoặc trích dẫn rõ nguồn bài viết khi sử dụng.

User avatar
NTN
Thành viên tâm huyết
Thành viên tâm huyết
Posts: 491
Joined: Wed 05/02/2014 3:43 pm
Location: Đồng Tháp,Việt Nam
Has thanked: 20 times
Been thanked: 8 times
Contact:

Re: Tính mã MD5 cho tệp tin

Postby NTN » Fri 07/02/2014 6:54 pm

Bạn nói vậy mả MD5 nó đúng không ?

User avatar
NTN
Thành viên tâm huyết
Thành viên tâm huyết
Posts: 491
Joined: Wed 05/02/2014 3:43 pm
Location: Đồng Tháp,Việt Nam
Has thanked: 20 times
Been thanked: 8 times
Contact:

Re: Tính mã MD5 cho tệp tin

Postby NTN » Fri 07/02/2014 7:05 pm

Đây là File tìm MD5 của các File như trên của bạn tuyen_dt18
Mình có thêm Label2 cho nó đẹp nhé !
Attachments
MD5.rar
Tập tin của bạn đây !
(26.67 KiB) Downloaded 263 times


Return to “[VB] Bài viết hướng dẫn”

Who is online

Users browsing this forum: No registered users and 1 guest