generateSaltedKey()
Signature
Section titled “Signature”SaltyKeys.generateSaltedKey(apiKey: string): string | nullParameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
apiKey | string | Your original, plain-text API key. Must be a non-empty string. |
Returns
Section titled “Returns”| Value | Meaning |
|---|---|
string | The obfuscated salted key. Store this in your pen code. |
null | Generation failed — either apiKey was invalid, the pen ID could not be extracted, or an encoding error occurred. |
How the salted key is constructed
Section titled “How the salted key is constructed”ORIGINAL_KEY + ":" + PEN_ID + ":" + TIMESTAMP + ":" + NONCE → base64 encoded → reversed character-by-character = salted keyEach component adds an obfuscation layer:
| Component | Purpose |
|---|---|
ORIGINAL_KEY | The value you want to protect |
PEN_ID | Binds the key to this exact pen |
TIMESTAMP | Makes each generated key unique in time |
NONCE | Random string adding unpredictability |
Example
Section titled “Example”Step 1 — Open your pen in CodePen and paste SaltyKeys.js. Then, in the browser console, run:
const salted = SaltyKeys.generateSaltedKey('MY_API_KEY_12345');console.log(salted);// → something like: "=kJHGFcba...reversed base64 string..."Step 2 — Copy the logged value and embed it in your pen’s JS:
const SALTED = 'PASTE_SALTED_KEY_HERE';const key = SaltyKeys.getApiKey(SALTED);Error cases
Section titled “Error cases”| Situation | Return value | Console warning |
|---|---|---|
apiKey is empty or not a string | null | "API key must be a non-empty string." |
| Pen ID could not be extracted | null | "Unable to extract Pen ID." |
| Encoding failed internally | null | "Failed to generate salted key: ..." |