Author Topic: Extract file Checksum  (Read 19292 times)

0 Members and 1 Guest are viewing this topic.

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Extract file Checksum
« Reply #15 on: September 09, 2016, 12:17:39 AM »
Now with input parameters helper, with available parameters defined from the script itself. ;)
Code: [Select]
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 ;)

Padanges

  • Newbie
  • *
  • Posts: 179
Re: Extract file Checksum
« Reply #16 on: September 23, 2016, 09:33:10 PM »
Code: [Select]
BatchFile.BookmarkRoot; //trick to trigger the file extraction from the archive to the system temporary foldercould you please explain why we need this "trick"? What's behind this? Why explicitly BookmarkRoot? Got me curious ;D

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Extract file Checksum
« Reply #17 on: September 23, 2016, 11:35:02 PM »
Code: [Select]
BatchFile.BookmarkRoot; //trick to trigger the file extraction from the archive to the system temporary foldercould you please explain why we need this "trick"? What's behind this? Why explicitly BookmarkRoot? Got me curious ;D
To calculate a file hash we need to read the file content. Archived files are only extracted when the scripts API needs to access the file itself, so any method that accesses the file will work for this propose. BookmarkRoot is just one of these methods.