Sha-256 hashing and Base64 encryption has a other result than expected

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gamemap

I want to connect a programm to the OBS Websocket Plugin and need to encrypt the password to match the encryption of the plugin as an authentification. Here is explained how I have to encrypt the password but ony the encryption from this Site worked.

I have to generate a binary SHA256 hash and encode the result to Base64 but this code won’t work: Marshalls.utf8_to_base64( str((passwort+salt).sha256_text() ) )

Example Strings with expected output:

  1. Data from OBS Websocket: {"authRequired":true,"challenge":"l5BYHeT6ajjiU50aAt97PKzvYJXMVCOwjRn4AWRylXk=","message-id":"auth_request","salt":"Ue+UWGL1GUN4b7SWTcGJ+7b9rjwmWtRVdOIqzt5qfkc=","status":"ok"}
    Password: “testtest”
  2. password+salt → binary_sha256 → Base64 encryption
  3. result from 2. +challenge → binary_sha256 → Base64 encryption

Result from 2. (password+salt): zoa5uikt6WyiwaKU3PMVj+TYao+miy3hyQ6HisYXt20=
Result from 3. (2. + challenge): VXGXqUbOxTwxICHvnObyZv9+2o4OLuXH5qxJ7AupzaY=

The 3rd result is the final and worked but only with the website and not with Godot.

I hope someone can explaine where the mistake in this code is.
Unfortunately i am not familiar with encryption and hashing.

:bust_in_silhouette: Reply From: aipie

I’m not sure, but I believe the problem is that you are using the .sha256_text() function.
This already converts your sha256 binary to a string, but I’m not sure if this is indeed utf8.

Could you try again but with the binary version instead.

var salt = "Ue+UWGL1GUN4b7SWTcGJ+7b9rjwmWtRVdOIqzt5qfkc="
var challenge = "l5BYHeT6ajjiU50aAt97PKzvYJXMVCOwjRn4AWRylXk="
var password = "testtest"

var two = Marshalls.raw_to_base64((password+salt).sha256_buffer())
var three = Marshalls.raw_to_base64((two+challenge).sha256_buffer())
print(two) # zoa5uikt6WyiwaKU3PMVj+TYao+miy3hyQ6HisYXt20=
print(three) # VXGXqUbOxTwxICHvnObyZv9+2o4OLuXH5qxJ7AupzaY=

Thank you very much, that solved my problem!

Gamemap | 2021-06-15 20:22