# Hashed password for serial device

**URL:** https://discourse.nodered.org/t/hashed-password-for-serial-device/30889
**Category:** General
**Created:** [29 July 2020 18:10 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889 "2020-07-29T18:10:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)
#### Post date: [29 July 2020 18:10 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889/1 "2020-07-29T18:10:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)
#### Post date: [30 July 2020 08:29 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889/2 "2020-07-30T08:29:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)
#### Post date: [30 July 2020 11:43 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889/3 "2020-07-30T11:43:38Z")

</div>

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:

```auto
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;

```

---

<div class="post-metadata">

### Author: ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)
#### Post date: [30 July 2020 18:54 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889/4 "2020-07-30T18:54:40Z")

</div>

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!!

---

<div class="post-metadata">

### Author: ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)
#### Post date: [31 July 2020 10:53 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889/5 "2020-07-31T10:53:27Z")

</div>

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);  
> }

---

<div class="post-metadata">

### Author: ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)
#### Post date: [31 July 2020 11:30 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889/6 "2020-07-31T11:30:16Z")

</div>

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 ;))

---

<div class="post-metadata">

### Author: ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)
#### Post date: [31 July 2020 11:46 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889/7 "2020-07-31T11:46:21Z")

</div>

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 😅

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

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.

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

```

thats what you would have to implement yourself.

probably in JS something like

```auto
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?

---

<div class="post-metadata">

### Author: ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)
#### Post date: [1 August 2020 15:53 UTC](https://discourse.nodered.org/t/hashed-password-for-serial-device/30889/8 "2020-08-01T15:53:45Z")

</div>

Did you get it going?
