Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change salt in precomputed test data #101

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

typotter
Copy link
Collaborator

  • Updated the salt string; salts should only be characters in the base64 "alphabet"
  • removed the base64 en/decoding of salt
  • Ran @sameerank's generation script (below) to convert the "deobfuscated" structure into the expected obfuscated structure.

configuration-wire/generate.ts

import {createHash} from "node:crypto";

// npx ts-node generate.ts

function hashAndEncode(jsonData: { precomputed: { response: any; }; }) {
    const salt = jsonData.precomputed.response.salt;
    const flags = jsonData.precomputed.response.flags;

    // Process each flag
    for (const [flagKey, flag] of Object.entries(flags)) {
        // Hash flag key with salt
        const hashedKey = createHash('md5')
            .update(salt + flagKey)
            .digest('hex');

        // Base64 encode specific fields
        // @ts-ignore
        flag.allocationKey = Buffer.from(flag.allocationKey).toString('base64');
        // @ts-ignore
        flag.variationKey = Buffer.from(flag.variationKey).toString('base64');
        // @ts-ignore
        flag.variationValue = Buffer.from(String(flag.variationValue)).toString('base64');

        // Process extraLogging if it has properties
        // @ts-ignore
        if (flag.extraLogging && Object.keys(flag.extraLogging).length > 0) {
            const newExtraLogging = {};
            // @ts-ignore
            for (const [key, value] of Object.entries(flag.extraLogging)) {
                const encodedKey = Buffer.from(key).toString('base64');
                const encodedValue = Buffer.from(String(value)).toString('base64');
                // @ts-ignore
                newExtraLogging[encodedKey] = encodedValue;
            }
            // @ts-ignore
            flag.extraLogging = newExtraLogging;
        }

        // Replace the original flag key with hashed key
        flags[hashedKey] = flag;
        delete flags[flagKey];
    }

    // Set obfuscated to true and stringify flags
    jsonData.precomputed.response.salt = salt;
    jsonData.precomputed.response.obfuscated = true;
    jsonData.precomputed.response = JSON.stringify(jsonData.precomputed.response);

    return jsonData;
}

const fs = require('fs');
const inputFile = 'precomputed-v1-deobfuscated.json';
const outputFile ='precomputed-v1.json';

const jsonData = JSON.parse(fs.readFileSync(inputFile, 'utf8'));
const encodedData = hashAndEncode(jsonData);
fs.writeFileSync(outputFile, JSON.stringify(encodedData, null, 2));

@typotter typotter merged commit fe0d818 into main Dec 17, 2024
3 checks passed
@typotter typotter deleted the tp/precomputed/nonencoded-salt branch December 17, 2024 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants