利用 WindowsInstaller.Installer 对象计算文件 MD5 hash 值

标签: , , , ,

曾经写过一篇《用VBS实现PHP的md5_file函数》,里面给出的 md5_file 函数可以用来计算文件的 MD5 hash 值。

但是 md5_file 函数调用了 CAPICOM 组件,而系统默认是没有安装这个组件的。无意中看到 UMU 大神的《利用 WindowsInstaller.Installer 对象计算文件 MD5 hash 值》,里面用到的是 WindowsInstaller.Installer 对象,不知道这个对象是不是系统自带的,反正我的系统上有。

' 40_FileHash.VBS

' UMU @ 22:45 2011/8/10

Option Explicit

Dim wi
Dim file
Dim file_size
Dim file_attributes
Dim file_version
Dim file_hash

Set wi = CreateObject("WindowsInstaller.Installer")
file = "C:\WINDOWS\explorer.exe"
file_size = wi.FileSize(file)
file_attributes = wi.FileAttributes(file)
file_version = wi.FileVersion(file)
file_hash = GetFileHash(file)

Set wi = Nothing

MsgBox "File: " & file & vbCrLf & _
"Size: " & file_size & vbCrLf & _
"Attributes: " & file_attributes & vbCrLf & _
"Version: " & file_version & vbCrLf & _
"MD5: " & file_hash

Function GetFileHash(file_name)
    Dim file_hash
    Dim hash_value
    Dim i
    
    Set file_hash = wi.FileHash(file_name, 0)
    
    hash_value = ""
    
    For i = 1 To file_hash.FieldCount
        hash_value = hash_value & BigEndianHex(file_hash.IntegerData(i))
    Next
    
    GetFileHash = hash_value
    
    Set file_hash = Nothing
End Function

Function BigEndianHex(Int)
    Dim result
    Dim b1, b2, b3, b4
    
    result = Hex(Int)
    b1 = Mid(result, 7, 2)
    b2 = Mid(result, 5, 2)
    b3 = Mid(result, 3, 2)
    b4 = Mid(result, 1, 2)
    
    BigEndianHex = b1 & b2 & b3 & b4
End Function

原文链接:http://hi.baidu.com/umu618/blog/item/d2772e1f6e0c0bd2a686697f.html

赞赏

微信赞赏支付宝赞赏

随机文章:

  1. Windows下编译cURL
  2. PHP调用COM组件
  3. CRC批量校验工具——RapidCRC
  4. PT流量作弊工具之PTLiar2
  5. VBS的一个BUG

5 条评论 发表在“利用 WindowsInstaller.Installer 对象计算文件 MD5 hash 值”上

  1. 艳文说道:

    要一个多星期不上网了,过来留言。。嘿嘿。。

  2. still说道:

    还没回家!!

  3. suN说道:

    新年快乐~

  4. osmond说道:

    这个md5值不准确啊,和其他几个md5工具加密的比较,那几个工具加密的结果都一样,唯独vbs加密的不一样

  5. osmond说道:

    哈,找到错误在哪里了,博主更新吧,貌似网上还没人关注这个,大家都在转帖
    BigEndianHex里没有对result的长度做判断,如果长度不够8位的话前面要补0的,而这位UMU大神不知怎么忽略了这个,希望博主更正

留下回复