How to hash by sha256

Hi my dear friends
Can anyone please help me how can I hash a string by sha256 by function node please?

Best regards;

What is your environment? Windows/Docker/Linux?

based on that you can use

So you can use the Execute Command node to get that going.

Not sure if that works, but nodejs comes with a crypto package that also includes sha256

https://nodejs.org/api/crypto.html#crypto_crypto

This should work on a function node if I’m not mistaken. Correct me if I’m wrong @jan

Dear @vuchl I use n8n on windows platform But as @Libermentixhas said I want some codes to use it in function node.

As you told I use that code in a function node and it show below error:

ERROR: Access denied to require ‘crypto’

I don’t think you can require the crypto module. But mayber there is a possibility to whitelist certain stuff from the standard library

@Libermentix @mooghermez That is sadly not possible as the Function-Node runs in a Sandbox environment and that does not allow to require external modules. I should maybe really add the option to run n8n in something like an “insecure-mode” which allows to do something like that.

Or you can write an external node script that can require the crypto module and call this via Execute Command. Allthough you want to use the Function node

to be honest, maybe a separate NodeJS-Code-Node would make the most sense. A sandboxed FunctionItem and Function-Node is a great tool for transformations. Making a NodeJS-Code Node without any restrictions would make the feature more explicit, in the sense of: “You need to know what you are doing”

Opinions?

What differences would there be between the NodeJS-Code-Node and the Function-Node? Just that it is not sandboxed and it would do more or less an eval?

I guess so. But users should be aware (maybe also a toggle-switch on the Function-Node would be sufficient – if the architecture allows this) that it is the ‘danger’ zone. Yet, it would open a lot of new possibilities because we could use the whole nodejs stack.

1 Like

Because I use google sheets for my database and some limited in sandbox I think I should search for hashing there, isn’t it?

@mooghermez If it is possible, I would maybe the easiest right now. But I do not know if they have a hash-function in Google Sheets.

Hoooooray :partying_face::partying_face::partying_face::partying_face:
I can do it.

In script editor I add below code:

function SHA256 (input) {
  var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, input);
  var txtHash = '';
  for (i = 0; i < rawHash.length; i++) {
    var hashVal = rawHash[i];
    if (hashVal < 0) {
      hashVal += 256;
    }
    if (hashVal.toString(16).length == 1) {
      txtHash += '0';
    }
    txtHash += hashVal.toString(16);
  }
  return txtHash;
}

then in cell I use SHA256(A1)

1 Like