Forum Discussion

team_aras_meupe_com's avatar
3 years ago

HASH File. how i can get??

Hello, I am trying to get the HASH value of a file. The fact, is that I want, before a user uploads a file, to check that that file has not been previously uploaded.
I have looked in ARAS and the system already saves that value in "checksum" itemtype "file", but it does not do any checking. This entire process is done in the file "xxhash-asm.min.js", which is in innovator\client\vendors.
But the line where it calculates it (line2) is not tabulated and has 132,387 characters, and I am not clear. I have tried other methods, to make myself a method, but it fails on this line: "crypto.subtle.digest('SHA-256' , data).then(function (hashBuffer) {"

Any suggestions, I suppose there must be an easier way to calculate/extract the HASH of a file.

 

 

My method:

var innovator = new Innovator();

aras.vault.selectFile().then(function (fileObject) {
  file = fileObject;

  var reader = new FileReader();
  reader.readAsText(file);

  // Aquí calculamos el hash del contenido del archivo
  reader.onload = function (readerEvent) {
    var content = readerEvent.target.result; // Este es el contenido del archivo

    // Calcular el hash del contenido del archivo
    calcularHashDeContenido(content, function (hash) {
      alert('Hash del archivo:', hash);
    });
  };
});

// Función para calcular el hash de un contenido
function calcularHashDeContenido(content, callback) {

  const crypto = window.crypto || window.msCrypto; // Obtener la implementación de crypto
  const encoder = new TextEncoder();
  const data = encoder.encode(content);
alert(1);
  crypto.subtle.digest('SHA-256', data).then(function (hashBuffer) {
alert(2);      
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
    callback(hashHex);
  });
}

I see in js files in ARAS Directory, that exist "getFileChecksum(fileName);", but I can't get it to work.

This is in "\Aras\Innovator\Innovator\Client\javascript\Aras\Client\Controls\Public" file Vault.js

4 Replies

  • Ok, as I thought it was easier than what I was trying.

    With "getFileChecksum" we get the value from the HASH without any problem, but we have to pass the value through a "file object" and I don't know how to pass the filepath through a variable, for example "filename".

    [embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:3b915065-7661-4a3f-882f-c7425b911cfe:type=text&text=aras.vault.selectFile%28%29.then%28function%28file%29%20%7B%0A%09alert%28%20aras.vault.getFileChecksum%28file%29%29%3B%0A%09%0A%09%0A%20%20%20%20var%20fileName%20%3D%20%22C%3A%5C%5CUsers%5C%5Cfernando.coronil%5C%5CDownloads%5C%5CPrueba.TXT%22%3B%0A%09alert%28%20aras.vault.getFileChecksum%28fileName%29%29%3B%0A%09%0A%09%7D%29%3B%0A]

    Line 1-2 works fine.

    Line 5-6 i get an error: "undefined".

    I try too to load the file object in the itemType "Document":

    But nothing. i dont know how i must to get this as an file object. [emoticon:6f61a5a028b641cb925f8551a1059cc2]

    • angela's avatar
      angela
      Catalyst II

      Hi,

      great use case! I tried something similar 7 years ago, but only tried to match names and inform the user that duplicates exists. But I never used the draft.

      It´s true that Innovator doesn´t use the checksum in any way. 

      Where do you trigger your aras.vault.select function? Do you currently only use the Method editor? Or did you overwrite the relationship file selector?

      When your function is just used in a JS Method, the "undefined" error makes sense. You use aras.vault to pick a file. "File" is your fileobject. You use it in the alert to get the checksum. So far everything fine.

      Then you write a path to a local file and use this one to get the checksum. This will not work, at least not in todays world (previous browsers from the 90s had some options...). JS itself cannot read local content automatically. That´s a security constraint. Just imagine every website in the world would have the potential power to read your local desktop content. So JS can only work with files, actively provided by the user with a file picker. 

      There are more flexible upload file options available than the aras.vault selector. But it depends on your overall usecase. At which point of the upload process do you want to do the checksum check? 

      • team_aras_meupe_com's avatar
        team_aras_meupe_com
        Creator III

        Hello Angela, thanks to write!!!.

        [quote userid="4412" url="~/f/development/38090/hash-file-how-i-can-get/9675"]great use case! I tried something similar 7 years ago, but only tried to match names and inform the user that duplicates exists. But I never used the draft.

        Perfect, if I get it to work I'll leave you the code in case it helps the community.

        [quote userid="4412" url="~/f/development/38090/hash-file-how-i-can-get/9675"]It´s true that Innovator doesn´t use the checksum in any way. 

        It is strange, since in the itemtype "Document", there is the checksum attribute, which is in "File" and as foreign in "Document File". The most logical thing, and even more so if there are many users, is that it will control in some way so that duplicate files are not uploaded.

        Right now, in my version of ARAS, when you press the save button "save item", icon of a 3 1/2 floppy disk, from my time, hahaha. ARAS calculates the checksum and saves it. But as I mentioned, he does it in the file "xxhash-asm.min.js", in the line 2, but it is not tabulated and has 132,387 characters, why ARAS???.
        From what I have seen, it does this if a file is added and it is saved, if 2 files are added it does not calculate the checksum, I don't know why!

        We had thought that the verification would be done when the user clicks on saving the Item. Since you can add the file in two ways, adding using the button, or doing a drag and drop in the grid.
        Ideally, if it is through the add button it should not be a problem, since the dialog box appears, but if it is dragged, it should handle the event. ARAS already executes code, since it adds the name of the file.

        If not, then we'll see if we put the check right after saving, to notify the user and edit the line. But ideally it would not be able to save or upload the file.

        Regarding the "limitation" of passing the file path, it is logical and after 2 days and reading a thousand sites I was imagining it. If it weren't like that it would be a waste!!

        Well, I'm going to see how I can take the file from the "Files" grid, if possible, and call getFileChecksum.

        Thank you very much Angela!!!