Hashed password for serial device

Hi, i'm trying to create a node to read an old serial device (RS232), i need to crate a function node which will change the Hex input to a new output so the device will accept the password.
Example
This is the password seed generated from the device:
Seed = 73 8C FA 08 BF 6C 84 F6 (Response from serial device, this variable changes all the time)
Password = 41 42 43 44 30 30 32
New password = Password ^ seed (X0R), This gives:
32 CE B9 4C 8F 5C B4 C4
I then need to + the last bit to the XOR
last = New password[7]
Final = New Password + last &0FF= F6C47DC958B4682C

How do i get from 738CFA08BF6C84F6 to F6C47DC958B4682C using a function node

Hi,
there are some questions left:
how do your payloads come in (seed and password)?

do the come in as hex/dec? do they come in as one concatenated string? do they come in as an array of seven strings? thats what the message handling will be all about.

You got me courious to try this during lunchtime:

here is a function that does the basics of what you asked, the probably "hard" part:

seed = ["73", "8C", "FA", "08", "BF", "6C", "84", "F6"];
password = ["41", "42", "43", "44", "30", "30", "32"];
let passdec = [];
let seeddec = [];
let newpass = [];

//------------------------------------------------
// define function to convert between bases
function decToHex(num){
    var hex = "";
    var temp = num;
    table = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
    rest = temp%16;
    while(temp > 0){
        temp = Math.floor(temp / 16);
        if(temp > 1){
        hex = hex + table[temp];
        }else{
            hex  = hex + table[rest];
        }
    }


    return hex;
}

//--------------------------------------------------

for (i=0;i<seed.length;i++){
    seed[i] = "0x"+seed[i]; //by 0x[number] tell NR this is a hex number
    seeddec[i] = parseInt(seed[i]); //parse a decimal number
 
}

for (i=0;i<password.length;i++){
    passdec[i] = parseInt(password[i]); //parse the decimals from password but the are stored as binary anyway (no quantum computing ;)
    newpass[i] = seeddec[i]^passdec[i]; //do the XOR stuff
    newpass[i] = decToHex(newpass[i]); // convert to hex
    
}


msg.payload = {passdec:passdec, seeddec:seeddec, seedhex:seed, newpass:newpass};
return msg;
1 Like

Thanks for you quick response!!
How do the payloads come in? Only the seed comes in from the payload (hex), I then need to add the password and push through the function.

It comes as a in as hex (buffer) so...
Node Red (Ask for seed)........> (i have this command set)
Serial device <......[73 8C FA 08 BF 6C 84 F6]
Function node (Do the clever stuff i.e. add password, XOR, +bit, &0FF)....[F6 C4 7D C9 58 B4 68 2C]...>
Accepted<............
I will test your function and see how i get on!!

When i test this function i get "5A""A6""D1""24""A1""72""A4" as the output.

I feel close to the answer tho! this is function in C++

string ElsterEncrypt(string password, string seed)
{
//Convert hex string to byte array.
byte s = GXCommon.HexToBytes(seed, false);
byte crypted = new byte[8];
for (int pos = 0; pos != 8; ++pos)
{
crypted[pos] = (byte)(password[pos] ^ s[pos]);
}
int last = crypted[7];
for (int pos = 0; pos != 8; ++pos)
{
crypted[pos] = (byte)((crypted[pos] + last) & 0xFF);
last = crypted[pos];
}
return GXCommon.ToHex(crypted, false);
}

1 Like

Yes what I wrote you is no final hash function. I only did the conversion of hex to dec, the xor and reconversion to hex. Let's take the first position (seed[0]&password[0]) as an example.

Js is not intended to be c++ in many matters. It gifts the unnecessarity of declaring data types, but it costs not having built-in functions for many operations. so mathematical Programms can easily have 200% of code compared to c++ (in which I am absolutely no pro, like in any language ;))

1 Like

73hex is 7*16+3 = 115dec which ist correct
41dec is 41dec.
115dec =
1 1 1 0 0 1 1 bin
0 1 0 1 0 0 1bin
41 dec =
XOR: 1011010
which is 2+8+16+64 = 90
90 = 5 x16+10 = 5Ahex
which is correct :sweat_smile:

or did i understand something wrong?
at least it works like i understood it :slight_smile:

what i didnt implement was the 0xFF append.

JS will fill missing zeros automatically when xor#ing decimals to byte, so thos cant be the problem.
you are doing the steps in your c++ positionwise, as i expected. so that cant be the problem either.

what i didnt do in the function is this.

crypted[pos] = (byte)((crypted[pos] + last) & 0xFF);

thats what you would have to implement yourself.

probably in JS something like

last = newpass[newpass.length-1]; // ==newpass[7]
for(i=0;i<8;i++){
newpass[i] = newpass[i] + last;
//and then
newpass[i] = decToHex(newpass[i]);:
}

looks like a checksum, right?

Did you get it going?