生成文件的哈希值 [英] Generating the hash value of a file

查看:233
本文介绍了生成文件的哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法收集代码并尝试生成文件的哈希值,但在当前代码中,我需要将文件拖到VBScript上,然后给出哈希值。



有人可以帮我重写代码,我可以选择文件夹或一组文件,并且可以生成散列值并将其写入记事本文件中。



附加下面的代码。

Dim objFile,objFolder,objFSO
Dim Arg,strText

strText =
Set objFSO = CreateObject(Scripting.FileSystemObject)

如果WScript.Arguments.Count> 0然后
对于Wscript.Arguments中的每个Arg
Arg = Trim(Arg)
如果InStr(Arg,。)然后
strText = strText& 文件名:& Arg& vbNewLine
如果doMd5那么
strText = strText& MD5 - >& md5(Arg)& vbNewLine
结束如果
结束如果
下一个
结束如果

'= 0参数表示使用双击md5.vbs(或可能通过没有文件名参数的命令行)

Dim fName
如果WScript.Arguments.Count = 0那么
fName = ChooseFile(。)
如果fName<> ; 然后
strText = strText& 文件名:& fName& vbNewLine
如果doMd5那么
strText = strText& MD5 - >& md5(fName)& vbNewLine
End If
Wscript.echo strText'需要这样才能防止事情在向记事本插入数据时变得疯狂(确保记事本是顶部窗口)
End If
End If

'如果用户在打开的文件对话框中取消文件选择,则优雅地退出
如果strText =然后
Dim strExit
strExit =没有选择文件,优雅地退出...和& vbNewLine
strExit = strExit +别忘了你也可以将文件拖放到这个脚本上。 &安培; vbNewLine
strExit = strExit +或者使用脚本中详细描述的发送到右键上下文菜单。 &安培; vbNewLine
MsgBox strExit,0,MD5.VBS
WScript.Quit
End If
Dim WshShell

Set WshShell = WScript.CreateObject( WScript.Shell)
WshShell.Runnotepad,3

WScript.Sleep 500

WshShell.SendKeys strText

函数md5(文件名)
默认MSXML,EL,MD5Obj

设置MD5Obj = CreateObject(System.Security.Cryptography.MD5CryptoServiceProvider)
MD5Obj.ComputeHash_2(readBinaryFile(filename))

Set MSXML = CreateObject(MSXML2.DOMDocument)
Set EL = MSXML.CreateElement(tmp)
EL.DataType =bin.hex
EL.NodeTypedValue = MD5Obj.Hash
md5 = EL.Text
End Function

函数readBinaryFile(文件名)
Const adTypeBinary = 1
Dim objStream
Set objStream = CreateObject(ADODB.Stream)
objStream.Type = adTypeBinary
objStream.Open
如果文件名<> 然后
objStream.LoadFromFile文件名'稍作修改以防止错误消息如果没有选择文件
结束如果
readBinaryFile = objStream.Read
objStream.Close
Set objStream = Nothing
End Function

Dim shell,defaultLocalDir,objWMIService,colItems,objItem,ex

Set shell = CreateObject(WScript.Shell)
defaultLocalDir = shell.ExpandEnvironmentStrings(%USERPROFILE%)& \Desktop
Set shell = Nothing

函数ChooseFile(ByVal initialDir)
Set objWMIService = GetObject(winmgmts:\\.\root\cimv2 )

Set colItems = objWMIService.ExecQuery(Select * from Win32_OperatingSystem)
Dim winVersion

winVersion = CInt(Left(objItem.version,1) )
Next
Set objWMIService = Nothing
Set colItems = Nothing

如果(winVersion< = 5)则
Set cd = CreateObject(UserAccounts .CommonDialog)
cd.InitialDir = initialDir
cd.Filter =ZIP文件| * .zip |文本文档| * .txt | Shell脚本| *。* sh |所有文件| *。*

cd.FilterIndex = 4
If cd.ShowOpen = True Then
ChooseFile = cd.FileName
Else
ChooseFile =
End If
Set cd = Nothing
其他
Set shell = CreateObject(WScript.Shell)
Set ex = shell .Exec(mshta.exeabout:)
ChooseFile = Replace(ex.StdOut.ReadAll,vbCRLF,)

Set ex = Nothing
Set shell = Nothing
End If
End Function


解决方案

  Set fso = CreateObject(Scripting.FileSystemObject)
Dim oMD5 :Set oMD5 = CreateObject(System.Security.Cryptography.MD5CryptoServiceProvider)
Dim oLog'As Scripting.TextStream

Set oArgs = WScript.Arguments

If oArgs.Count = 1然后
sFolderPath = GetFolderPath()
设置oLog = fso.CreateTextFile(sFolderPath& \FileHash.csv,True)
oLog.Writesep =& vbTab& vbCrLf
CheckFolder oArgs(I)
oLog.Close
Msgbox完成!
Else
MsgboxDrop Folder
End If

Sub CheckFolder(sFolderPath)
Dim sKey
Dim oFolder'As Scripting.Folder
设置oFolder = fso.GetFolder(sFolderPath)

对于oFolder.Files中的每个oFile
oLog.Write oFile.Path& vbTab& GetMd5(oFile.Path)& vbCrLf
下一个

对于每个oChildFolder在oFolder.SubFolders
CheckFolder oChildFolder.Path
下一个
End Sub

函数GetFolderPath ()
Dim oFile'As Scripting.File
Set oFile = fso.GetFile(WScript.ScriptFullName)
GetFolderPath = oFile.ParentFolder
End Function

函数GetMd5(文件名)
Dim oXml,oElement

oMD5.ComputeHash_2(GetBinaryFile(filename))

Set oXml = CreateObject(MSXML2.DOMDocument)
Set oElement = oXml.CreateElement(tmp)
oElement.DataType =bin.hex
oElement.NodeTypedValue = oMD5.Hash
GetMd5 = oElement.Text
End Function
$ b $函数GetBinaryFile(文件名)
Dim oStream:Set oStream = CreateObject(ADODB.Stream)
oStream.Type = 1'adTypeBinary
oStream.Open
oStream.LoadFromFile文件名
GetBinaryFile = oStream.Read
oStream.Close
设置o Stream = Nothing
End Function


I have managed to gather code and tried to generate the hash value of a file, but in the present code I need to drag the file on the VBScript, then it gives the hash value.

Can someone help me in re-writing the code where I can select the folder or a group of files and the hash values can be generated and written in the notepad file.

Attaching the code below.

Dim objFile,objFolder,objFSO
Dim Arg, strText

strText = ""
Set objFSO = CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count > 0 Then
    For Each Arg in Wscript.Arguments
        Arg = Trim(Arg)
        If InStr(Arg,".") Then
            strText = strText & "Filename: " & Arg & vbNewLine
            If doMd5 Then
                strText = strText & "MD5 --> " & md5(Arg) & vbNewLine
            End If
        End If
    Next
End If

' = 0 arguments means use double-clicked md5.vbs (or possible executed via the command line without filename arguments)

Dim fName
If WScript.Arguments.Count = 0 Then
    fName = ChooseFile(".")
    If fName <> "" Then
        strText = strText & "Filename: " & fName & vbNewLine
        If doMd5 Then
            strText = strText & "MD5 --> " & md5(fName) & vbNewLine
        End If
        Wscript.echo strText 'need this to keep things from going crazy when inserting data into notepad (ensures notepad is top window somehow)
    End If
End If

'exit gracefully if the user canceled file selection in the open file dialog
If strText = "" Then
    Dim strExit
    strExit = "No file selected, exiting gracefully..." & vbNewLine
    strExit = strExit + "Don't forget you can drag and drop files onto this script, too." & vbNewLine
    strExit = strExit + "Or use the 'Send To' right-context menu as detailed in the script." & vbNewLine
    MsgBox  strExit, 0, "MD5.VBS"
    WScript.Quit
End If
Dim WshShell

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad", 3

WScript.Sleep 500

WshShell.SendKeys strText

Function md5(filename)
    Dim MSXML, EL, MD5Obj

    Set MD5Obj = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
    MD5Obj.ComputeHash_2(readBinaryFile(filename))

    Set MSXML = CreateObject("MSXML2.DOMDocument")
    Set EL = MSXML.CreateElement("tmp")
    EL.DataType = "bin.hex"
    EL.NodeTypedValue = MD5Obj.Hash
    md5 = EL.Text
End Function

Function readBinaryFile(filename)
    Const adTypeBinary = 1
    Dim objStream
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = adTypeBinary
    objStream.Open
    If filename <> "" Then
        objStream.LoadFromFile filename  'slight modification here to prevent error msg if no file selected
    End If
    readBinaryFile = objStream.Read
    objStream.Close
    Set objStream = Nothing
End Function

Dim shell, defaultLocalDir, objWMIService, colItems, objItem, ex

Set shell = CreateObject( "WScript.Shell" )
defaultLocalDir = shell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop"
Set shell = Nothing

Function ChooseFile(ByVal initialDir)
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

    Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    Dim winVersion

    winVersion = CInt(Left(objItem.version, 1))
    Next
    Set objWMIService = Nothing
    Set colItems = Nothing

    If (winVersion <= 5) Then
        Set cd = CreateObject("UserAccounts.CommonDialog")
        cd.InitialDir = initialDir
        cd.Filter = "ZIP files|*.zip|Text Documents|*.txt|Shell Scripts|*.*sh|All Files|*.*"

        cd.FilterIndex = 4
        If cd.ShowOpen = True Then
            ChooseFile = cd.FileName
        Else
            ChooseFile = ""
        End If
        Set cd = Nothing
    Else
        Set shell = CreateObject( "WScript.Shell" )
        Set ex = shell.Exec( "mshta.exe ""about: """ )
        ChooseFile = Replace( ex.StdOut.ReadAll, vbCRLF, "" )

        Set ex = Nothing
        Set shell = Nothing
    End If
End Function

解决方案

Here we go:

Set fso = CreateObject("Scripting.FileSystemObject")
Dim oMD5:  Set oMD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
Dim oLog 'As Scripting.TextStream

Set oArgs = WScript.Arguments

If oArgs.Count = 1 Then
    sFolderPath = GetFolderPath()
    Set oLog = fso.CreateTextFile(sFolderPath & "\FileHash.csv", True)
    oLog.Write "sep=" & vbTab & vbCrLf
    CheckFolder oArgs(I)
    oLog.Close
    Msgbox "Done!"
Else
    Msgbox "Drop Folder"
End If

Sub CheckFolder(sFolderPath)
    Dim sKey
    Dim oFolder 'As Scripting.Folder
    Set oFolder = fso.GetFolder(sFolderPath)

    For Each oFile In oFolder.Files
        oLog.Write oFile.Path & vbTab & GetMd5(oFile.Path) & vbCrLf
    Next

    For Each oChildFolder In oFolder.SubFolders
        CheckFolder oChildFolder.Path
    Next
End Sub

Function GetFolderPath()
    Dim oFile 'As Scripting.File
    Set oFile = fso.GetFile(WScript.ScriptFullName)
    GetFolderPath = oFile.ParentFolder
End Function

Function GetMd5(filename)
    Dim oXml, oElement

    oMD5.ComputeHash_2(GetBinaryFile(filename))

    Set oXml = CreateObject("MSXML2.DOMDocument")
    Set oElement = oXml.CreateElement("tmp")
    oElement.DataType = "bin.hex"
    oElement.NodeTypedValue = oMD5.Hash
    GetMd5 = oElement.Text
End Function

Function GetBinaryFile(filename)
    Dim oStream: Set oStream = CreateObject("ADODB.Stream")
    oStream.Type = 1 'adTypeBinary
    oStream.Open
    oStream.LoadFromFile filename
    GetBinaryFile= oStream.Read
    oStream.Close
    Set oStream = Nothing
End Function

这篇关于生成文件的哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆