Created
March 29, 2026 12:24
-
-
Save gustavonovaes/314d4282feb08f2da1891f4c065fdd98 to your computer and use it in GitHub Desktop.
This code demonstrates how to use Unicode Variation Selectors to encode and decode hidden messages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This code demonstrates how to use Unicode Variation Selectors to encode and decode hidden messages. | |
| * | |
| * It defines two functions: `encoder` to convert a string into a sequence of Unicode characters that | |
| * represent the bytes of the original string, and `decoder` to reverse this process and retrieve the | |
| * original string from the hidden Unicode characters. | |
| * | |
| * The `encoder` function takes a string, converts it to a buffer of bytes, and then maps each byte to | |
| * a corresponding Unicode character. If the byte value is 15 or less, it uses the range U+FE00 to U+FE0F; | |
| * if it's greater than 15, it uses the range U+E0100 to U+E01EF. | |
| * | |
| * The `decoder` function takes the hidden Unicode string, extracts the code points of each character, and | |
| * determines whether they fall within the specified ranges. It then reconstructs the original byte values | |
| * and converts them back to a UTF-8 string. | |
| */ | |
| function encoder(text) { | |
| return [...Buffer.from(text, "utf-8")] | |
| .map((byte) => | |
| String.fromCodePoint(byte <= 15 ? 0xfe00 + byte : 0xe0100 + byte - 16), | |
| ) | |
| .join(""); | |
| } | |
| function decoder(hiddenText) { | |
| const bytes = [...hiddenText] | |
| .map((char) => { | |
| const w = char.codePointAt(0); | |
| if (w >= 0xfe00 && w <= 0xfe0f) return w - 0xfe00; | |
| if (w >= 0xe0100 && w <= 0xe01ef) return w - 0xe0100 + 16; | |
| return null; | |
| }) | |
| .filter((n) => n !== null); | |
| return Buffer.from(bytes).toString("utf-8"); | |
| } | |
| const payload = "Hello, World!"; | |
| const hidden = encoder(payload); | |
| const revealed = decoder(hidden); | |
| console.log("Payload:", payload); | |
| console.log("Hidden:", hidden); | |
| console.log("Revealed:", revealed); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment