Now with input parameters helper, with available parameters defined from the script itself.
function FileHashParams() {
return JSON.stringify({
params: [{
Method: ['md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd160']
}, {
LetterCase: ['lower', 'upper']
}]
});
}
var fso = new ActiveXObject("Scripting.FileSystemObject");
var EL;
function FileHash() {
try {
if (!EL) {
var MSXML = new ActiveXObject("MSXML2.DOMDocument");
EL = MSXML.createElement("tmp");
EL.dataType = "bin.hex";
}
var filename = BatchFile.Filename;
if (filename.indexOf('>') > 0) { //file is archived
filename = fso.getFileName(filename.substr(filename.indexOf('>') + 1)); //get archived file name, by removig the archive name and any internal path
filename = fso.BuildPath(fso.GetSpecialFolder(2 /*TemporaryFolder*/ ), filename);
BatchFile.BookmarkRoot; //trick to trigger the file extraction from the archive to the system temporary folder
if (!fso.FileExists(filename)) return "extraction from archive failed"
} else if (filename.length + 1 > 260 /*MAX_PATH*/ ) {
var f = fso.GetFile('\\\\?\\' + filename);
filename = f.ShortPath.substr(4);
}
try {
var params = JSON.parse(CurrentField.Value);
} catch (err) {
var params = {
Method: "md5"
}
}
EL.nodeTypedValue = GetFileHash(filename, params.Method);
if (params.LetterCase === 'upper') return EL.text.toLocaleUpperCase()
else return EL.text;
} catch (err) {
return err;
}
}
var stream;
function GetFileStream(filename) {
if (!stream) {
stream = new ActiveXObject("ADODB.Stream");
stream.Type = 1; //adTypeBinary
}
stream.Open();
stream.LoadFromFile(filename);
return stream
}
function GetFileHash(filename, hashtype) {
var HasMethodProgID = "";
switch (hashtype) {
case "md5":
HasMethodProgID = "System.Security.Cryptography.MD5CryptoServiceProvider";
break;
case "sha1":
HasMethodProgID = "System.Security.Cryptography.SHA1Managed";
break;
case "sha256":
HasMethodProgID = "System.Security.Cryptography.SHA256Managed";
break;
case "sha384":
HasMethodProgID = "System.Security.Cryptography.SHA384Managed";
break;
case "sha512":
HasMethodProgID = "System.Security.Cryptography.SHA512Managed";
break;
case "ripemd160":
HasMethodProgID = "System.Security.Cryptography.ripemd160Managed"
break;
}
if (!HasMethodProgID) throw '"' + hashtype + '" is not a valid hash method.';
try {
if (GetFileStream(filename)) return ComputeHash(stream, HasMethodProgID)
} catch (err) {
throw err
} finally {
if (stream && stream.state == 1) stream.Close();
}
}
function ComputeHash(stream, objectid) {
var BlockSize = 65536;
var HashObj = new ActiveXObject(objectid);
try {
var nBlocks = parseInt(stream.size / BlockSize);
var block;
for (var i = 1; i < nBlocks; i++) {
block = stream.read(BlockSize);
HashObj.TransformBlock(block, 0, BlockSize, block, 0);
}
var n = stream.size - stream.position;
block = stream.read(n);
HashObj.TransformFinalBlock(block, 0, n);
return HashObj.Hash;
} finally {
HashObj.Clear
}
}
It's a new functionality, doesn't work with the 1.5.66.2 version. Just teasing