I try to authenticate with the OBS Studio websocket. It seems like my authorization…
- User
I try to authenticate with the OBS Studio websocket. It seems like my authorization string is wrong. I implemented it based on the documentations but I could not find an explicit testcase to check if my algorithm produces the expected result. I have found a library that does the same but this is node.js and also does not have expicit strings as unit tests for authentication. I copy here both, give me advise My implementation for the browser const te = new TextEncoder(); const hashedSecret = await crypto.subtle.digest( "SHA-256", te.encode( this.password + salt ) ); const b64Secret = btoa( hashedSecret ); const hashedAuthResponse = await crypto.subtle.digest( "SHA-256", te.encode( b64Secret + challenge ) ); const authentication = btoa( hashedAuthResponse );
the library implementation for node.js const hash = Base64.stringify(sha256(msg + salt))!;
return Base64.stringify(sha256(hash + challenge))!; - Assistant
Great, let's break down your implementation and the Node.js library's implementation to see where things might be going wrong.
1. Node.js Library Implementation
const hash = Base64.stringify(sha256(msg + salt)); return Base64.stringify(sha256(hash + challenge));msgis the password.saltis the salt from OBS.challengeis the challenge from OBS.sha256()returns a byte array (digest).Base64.stringify()encodes the digest to base64.
So, the steps are:
hash = base64(sha256(password + salt))auth = base64(sha256(hash + challenge))
2. Your Browser Implementation
const te = new TextEncoder(); const hashedSecret = await crypto.subtle.digest("SHA-256", te.encode(this.password + salt)); const b64Secret = btoa(hashedSecret); const hashedAuthResponse = await crypto.subtle.digest("SHA-256", te.encode(b64Secret + challenge)); const authentication = btoa(hashedAuthResponse);
3. Issues in Your Implementation
A.
btoaexpects a string, not a byte arraycrypto.subtle.digestreturns anArrayBuffer, not a string.btoaexpects a string, not anArrayBufferorUint8Array.- Passing an
ArrayBufferdirectly tobtoawill not work as intended.
B. Base64 encoding of the hash
- You need to convert the hash (ArrayBuffer) to a base64 string correctly.
C. Concatenation
- When you do
te.encode(b64Secret + challenge), you are encoding the concatenated string, which is correct ifb64Secretis a proper base64 string.
4. Correct Way to Convert ArrayBuffer to Base64
Here's a helper function:
function arrayBufferToBase64(buffer) { let binary = ''; const bytes = new Uint8Array(buffer); for (let i = 0; i < bytes.byteLength; i++) { binary += String.fromCharCode(bytes[i]); } return btoa(binary); }
5. Corrected Implementation
const te = new TextEncoder(); // Step 1: SHA256(password + salt) const secret = this.password + salt; const hashedSecret = await crypto.subtle.digest("SHA-256", te.encode(secret)); const b64Secret = arrayBufferToBase64(hashedSecret); // Step 2: SHA256(base64(secretHash) + challenge) const authString = b64Secret + challenge; const hashedAuthResponse = await crypto.subtle.digest("SHA-256", te.encode(authString)); const authentication = arrayBufferToBase64(hashedAuthResponse);
6. Test Case
You can use the Node.js library to generate a test vector. For example, with:
- password:
test - salt:
1234 - challenge:
abcd
You can run the Node.js code and get the expected result, then check if your browser code matches.
7. Summary
- Do not use
btoadirectly on ArrayBuffer. - Convert ArrayBuffer to base64 properly.
- Follow the exact steps:
- SHA256(password + salt) → base64
- SHA256(base64 + challenge) → base64
If you want a ready-to-use function:
async function obsAuth(password, salt, challenge) { const te = new TextEncoder(); function arrayBufferToBase64(buffer) { let binary = ''; const bytes = new Uint8Array(buffer); for (let i = 0; i < bytes.byteLength; i++) { binary += String.fromCharCode(bytes[i]); } return btoa(binary); } const secret = password + salt; const hashedSecret = await crypto.subtle.digest("SHA-256", te.encode(secret)); const b64Secret = arrayBufferToBase64(hashedSecret); const authString = b64Secret + challenge; const hashedAuthResponse = await crypto.subtle.digest("SHA-256", te.encode(authString)); return arrayBufferToBase64(hashedAuthResponse); }
Test this function with known values from the Node.js library to verify correctness.
If you need a test vector, let me know your password, salt, and challenge, and I can generate the expected result for you!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.