Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

I try to authenticate with the OBS Studio websocket. It seems like my authorization…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Mistralmistral-medium-2505’s answer.
  1. 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))!;
    
  2. 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));
    
    • msg is the password.
    • salt is the salt from OBS.
    • challenge is the challenge from OBS.
    • sha256() returns a byte array (digest).
    • Base64.stringify() encodes the digest to base64.

    So, the steps are:

    1. hash = base64(sha256(password + salt))
    2. 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. btoa expects a string, not a byte array

    • crypto.subtle.digest returns an ArrayBuffer, not a string.
    • btoa expects a string, not an ArrayBuffer or Uint8Array.
    • Passing an ArrayBuffer directly to btoa will 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 if b64Secret is 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 btoa directly on ArrayBuffer.
    • Convert ArrayBuffer to base64 properly.
    • Follow the exact steps:
      1. SHA256(password + salt) → base64
      2. 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!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.