Hey all,
I've been using the CryptoJS library to encode a string to authenticate a user with another program. The way it works is that you send an auth request, and you get two strings. Then you combine and encode those strings with your password to generate your auth key.
You get: a salt and a challenge string.
- Concatenate password + salt, generate a SHA256 hash then encode the result to base64.
- Take the result of the first step and concatenate it with the challenge string.
- Take the new string you created in the second step, generate a SHA256 hash and then encode the result to base64. And there's your auth key.
It's a bit confusing, more info is available at https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md.
The problem I've been having is that despite the same salt and challenge strings, I get different results if I assign the strings to a variable vs when I manually put them in an inject node. I've tried assigning the strings to a message, to a flow variable and a global variable, and each time I get a different result despite the same strings every time. The two strings change whenever the program is relaunched, so they need to be read from the payload and can't be manually entered. My flow is below.
Thanks!
[{"id":"755f0b8.5893ff4","type":"inject","z":"59ca3f9a.a4f18","name":"Manual Entry","props":[{"p":"password","v":"password","vt":"str"},{"p":"salt","v":"aszng9OoS6ybw9yTW2WR4GySMNdM3srSQ4KE70aungY=","vt":"str"},{"p":"challenge","v":"KSO+hOFs1q5SkEnx8bvp67Om2zyHDD6ZJF4NHAa3R94=","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":160,"y":100,"wires":[["786daa7f.3fe284"]]},{"id":"36ed9ba2.56be04","type":"inject","z":"59ca3f9a.a4f18","name":"Variable","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"authRequired\": true, \"challenge\": \"KSO+hOFs1q5SkEnx8bvp67Om2zyHDD6ZJF4NHAa3R94=\", \"message-id\": \"f8100636.83edd8\", \"salt\": \"aszng9OoS6ybw9yTW2WR4GySMNdM3srSQ4KE70aungY=\", \"status\": \"ok\" }","payloadType":"json","x":130,"y":140,"wires":[["b699085c.1bbdd8"]]},{"id":"786daa7f.3fe284","type":"function","z":"59ca3f9a.a4f18","name":"Auth Function","func":"var CryptoJS = context.global.cryptojs;\n\nvar secret_hash = CryptoJS.SHA256(msg.password + msg.salt);\nvar secret = CryptoJS.enc.Base64.stringify(secret_hash);\n\nvar auth_response_hash = CryptoJS.SHA256(secret + msg.challenge);\nvar auth_response = CryptoJS.enc.Base64.stringify(auth_response_hash);\n\nmsg.payload = auth_response;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":120,"wires":[["b958d436.f24a48"]]},{"id":"b958d436.f24a48","type":"debug","z":"59ca3f9a.a4f18","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":120,"wires":[]},{"id":"b699085c.1bbdd8","type":"change","z":"59ca3f9a.a4f18","name":"Assign Variables","rules":[{"t":"set","p":"password","pt":"msg","to":"password","tot":"str"},{"t":"set","p":"salt","pt":"msg","to":"payload.salt","tot":"str"},{"t":"set","p":"challenge","pt":"msg","to":"payload.challenge","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":140,"wires":[["786daa7f.3fe284"]]}]