PDF Explorer > Ideas/Suggestions

Extract file Checksum

(1/4) > >>

Padanges:
Hi,
it would be useful to add Checksum values (MD5, SHA-1) to DB for each pdf file. Is there any way to accomplish that using PDFe version 1.55.6.2? If no, would you consider including that function?

Regards.

RTT:
Try with this vbscript script function. To be used the same way as explained in your other post about existence of attachments. This function must take a parameter to specify the hash algorithm.
This are the algorithms supported:
[CalcHash](md5)
[CalcHash](sha1)
[CalcHash](sha256)
[CalcHash](sha384)
[CalcHash](sha512)
[CalcHash](ripemd160)


--- Code: ---Function CalcHash()
 Select Case CurrentField.value
  Case "md5"        CalcHash=md5(BatchFile.filename)
  Case "sha1"       CalcHash=sha1(BatchFile.filename)
  Case "sha256"     CalcHash=sha256(BatchFile.filename)
  Case "sha384"     CalcHash=sha384(BatchFile.filename)
  Case "sha512"     CalcHash=sha512(BatchFile.filename)
  Case "ripemd160"  CalcHash=ripemd160(BatchFile.filename)       
  Case Else CalcHash=""
 End Select
End Function

'from http://mwganson.freeyellow.com/md5/
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 sha1(filename)
Dim MSXML, EL
Set SHA1 = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
SHA1.ComputeHash_2(readBinaryFile(filename))

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

Function sha256(filename)
Dim MSXML, EL
Set SHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")
SHA256.ComputeHash_2(readBinaryFile(filename))

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

Function sha384(filename)
Dim MSXML, EL
Set SHA384 = CreateObject("System.Security.Cryptography.SHA384Managed")
SHA384.ComputeHash_2(readBinaryFile(filename))

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


Function sha512(filename)
Dim MSXML, EL
Set SHA512 = CreateObject("System.Security.Cryptography.SHA512Managed")
SHA512.ComputeHash_2(readBinaryFile(filename))

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

Function ripemd160(filename)
Dim MSXML, EL
Set ripemd160 = CreateObject("System.Security.Cryptography.ripemd160Managed")
ripemd160.ComputeHash_2(readBinaryFile(filename))

Set MSXML = CreateObject("MSXML2.DOMDocument")
Set EL = MSXML.CreateElement("tmp")
EL.DataType = "bin.hex"
EL.NodeTypedValue = ripemd160.Hash
ripemd160 = 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 End If 'slight modification here to prevent error msg if no file selected
readBinaryFile = objStream.Read
objStream.Close
Set objStream = Nothing
End Function

--- End code ---

Padanges:
Thanks. The code works fine.
Is it possible to make equivalent checks/operations using jScript, and not vbScript?

RTT:
You may create the above code as a script in the libraries section of the scripts editor. Then you may call any of its functions from any script, even from a different language, by including its reference (i.e. checking its name in the list of includes) in that script.

In the "libraries" tab create a VBScript and call it HashUtils. Put there the above code, but replace the CalcHash function with this one:

--- Code: ---Function GetFileHash(filename,hashtype)
 Select Case hashtype
  Case "md5"        GetFileHash=md5(filename)
  Case "sha1"       GetFileHash=sha1(filename)
  Case "sha256"     GetFileHash=sha256(filename)
  Case "sha384"     GetFileHash=sha384(filename)
  Case "sha512"     GetFileHash=sha512(filename)
  Case "ripemd160"  GetFileHash=ripemd160(filename)       
  Case Else GetFileHash=""
 End Select
End Function

--- End code ---

Now, in the "scripts" tab, create a JScript named CalcHash with this code:

--- Code: ---function CalcHash() {
    return GetFileHash(BatchFile.filename, CurrentField.value);
}

--- End code ---
To reference the HashUtils library, that contains the implementation of GetFileHash function, click the includes button, the button at the right of the script language selector (check the second screenshot of this forum post for reference), and check the HashUtils entry. Now you have a jscript that is using functionalities from a VBScript. ;)

Take notice that I've just found a bug while testing this. You will need to close the batch tool, and reopen it, when creating/making changes to a script in the libraries, or the called script function will not work immediately after closing the editor.

Padanges:
Actually, each time I change code in any Script, it does not work immediately after closing the editor.
Strangely enough, when I run debug for the code on the Jscript side, it returns me a number as a command prompt answer. But the Edit Info batch tool returns only empty strings (even when I change return to "any text" on Case Esle in VBscript). Something's buggy here.

Navigation

[0] Message Index

[#] Next page

Reply

Go to full version