@vercel/stega 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vercelStegaDecode = exports.VERCEL_STEGA_REGEX = void 0;
4
+ const map_1 = require("./map");
5
+ const REVERSE_HEX_DIGIT_MAP = Object.fromEntries(Object.entries(map_1.HEX_DIGIT_MAP).map((x) => x.reverse()));
6
+ const HEX_CHARS = `${Object.values(map_1.HEX_DIGIT_MAP)
7
+ .map((x) => `\\u{${x.toString(16)}}`)
8
+ .join('')}`;
9
+ exports.VERCEL_STEGA_REGEX = new RegExp(`(?:[${HEX_CHARS}]{2})+`, 'u');
10
+ /**
11
+ * Decodes a hidden string back into its original value
12
+ * @param encoded - The encoded (hidden) string
13
+ * @returns The decoded JSON value
14
+ */
15
+ function vercelStegaDecode(encoded) {
16
+ const match = encoded.match(exports.VERCEL_STEGA_REGEX);
17
+ if (!match)
18
+ return;
19
+ return decode(match[0]);
20
+ }
21
+ exports.vercelStegaDecode = vercelStegaDecode;
22
+ function decode(encodedString) {
23
+ const encoded = Array.from(encodedString);
24
+ if (encoded.length % 2) {
25
+ throw new Error(`Invalid encoded text length. Expected length to be even, received: ${encoded.length}`);
26
+ }
27
+ const chars = [];
28
+ for (let i = encoded.length * 0.5; i--;) {
29
+ const hex = `${REVERSE_HEX_DIGIT_MAP[encoded[i * 2].codePointAt(0)]}${REVERSE_HEX_DIGIT_MAP[encoded[i * 2 + 1].codePointAt(0)]}`;
30
+ chars.unshift(String.fromCharCode(parseInt(hex, 16)));
31
+ }
32
+ return JSON.parse(chars.join(''));
33
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vercelStegaEncode = void 0;
4
+ const map_1 = require("./map");
5
+ /**
6
+ * Encodes JSON as a hidden string
7
+ * @param json - The JSON data to encode
8
+ * @returns The hidden string
9
+ */
10
+ function vercelStegaEncode(json) {
11
+ const string = JSON.stringify(json);
12
+ return Array.from(string)
13
+ .map((character) => {
14
+ const charCode = character.charCodeAt(0);
15
+ if (charCode > 255) {
16
+ throw new Error(`Only ASCII edit info can be encoded. Error attempting to encode ${string} on character ${character} (${charCode})`);
17
+ }
18
+ return Array.from(charCode.toString(16).padStart(2, '0'))
19
+ .map((hex) => String.fromCodePoint(map_1.HEX_DIGIT_MAP[hex]))
20
+ .join('');
21
+ })
22
+ .join('');
23
+ }
24
+ exports.vercelStegaEncode = vercelStegaEncode;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./encode"), exports);
18
+ __exportStar(require("./decode"), exports);
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HEX_DIGIT_MAP = void 0;
4
+ exports.HEX_DIGIT_MAP = {
5
+ 0: 0x200b,
6
+ 1: 0x200c,
7
+ 2: 0x200d,
8
+ 3: 0x2062,
9
+ 4: 0x2063,
10
+ 5: 0x2060,
11
+ 6: 0xfeff,
12
+ 7: 0x2061,
13
+ 8: 0x1d173,
14
+ 9: 0x1d174,
15
+ a: 0x1d175,
16
+ b: 0x1d176,
17
+ c: 0x1d177,
18
+ d: 0x1d178,
19
+ e: 0x1d179,
20
+ f: 0x1d17a,
21
+ };
@@ -0,0 +1,7 @@
1
+ export declare const VERCEL_STEGA_REGEX: RegExp;
2
+ /**
3
+ * Decodes a hidden string back into its original value
4
+ * @param encoded - The encoded (hidden) string
5
+ * @returns The decoded JSON value
6
+ */
7
+ export declare function vercelStegaDecode<T>(encoded: string): T | undefined;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Encodes JSON as a hidden string
3
+ * @param json - The JSON data to encode
4
+ * @returns The hidden string
5
+ */
6
+ export declare function vercelStegaEncode<T>(json: T): string;
@@ -0,0 +1,29 @@
1
+ import { HEX_DIGIT_MAP } from './map';
2
+ const REVERSE_HEX_DIGIT_MAP = Object.fromEntries(Object.entries(HEX_DIGIT_MAP).map((x) => x.reverse()));
3
+ const HEX_CHARS = `${Object.values(HEX_DIGIT_MAP)
4
+ .map((x) => `\\u{${x.toString(16)}}`)
5
+ .join('')}`;
6
+ export const VERCEL_STEGA_REGEX = new RegExp(`(?:[${HEX_CHARS}]{2})+`, 'u');
7
+ /**
8
+ * Decodes a hidden string back into its original value
9
+ * @param encoded - The encoded (hidden) string
10
+ * @returns The decoded JSON value
11
+ */
12
+ export function vercelStegaDecode(encoded) {
13
+ const match = encoded.match(VERCEL_STEGA_REGEX);
14
+ if (!match)
15
+ return;
16
+ return decode(match[0]);
17
+ }
18
+ function decode(encodedString) {
19
+ const encoded = Array.from(encodedString);
20
+ if (encoded.length % 2) {
21
+ throw new Error(`Invalid encoded text length. Expected length to be even, received: ${encoded.length}`);
22
+ }
23
+ const chars = [];
24
+ for (let i = encoded.length * 0.5; i--;) {
25
+ const hex = `${REVERSE_HEX_DIGIT_MAP[encoded[i * 2].codePointAt(0)]}${REVERSE_HEX_DIGIT_MAP[encoded[i * 2 + 1].codePointAt(0)]}`;
26
+ chars.unshift(String.fromCharCode(parseInt(hex, 16)));
27
+ }
28
+ return JSON.parse(chars.join(''));
29
+ }
@@ -0,0 +1,20 @@
1
+ import { HEX_DIGIT_MAP } from './map';
2
+ /**
3
+ * Encodes JSON as a hidden string
4
+ * @param json - The JSON data to encode
5
+ * @returns The hidden string
6
+ */
7
+ export function vercelStegaEncode(json) {
8
+ const string = JSON.stringify(json);
9
+ return Array.from(string)
10
+ .map((character) => {
11
+ const charCode = character.charCodeAt(0);
12
+ if (charCode > 255) {
13
+ throw new Error(`Only ASCII edit info can be encoded. Error attempting to encode ${string} on character ${character} (${charCode})`);
14
+ }
15
+ return Array.from(charCode.toString(16).padStart(2, '0'))
16
+ .map((hex) => String.fromCodePoint(HEX_DIGIT_MAP[hex]))
17
+ .join('');
18
+ })
19
+ .join('');
20
+ }
@@ -0,0 +1,2 @@
1
+ export * from './encode';
2
+ export * from './decode';
@@ -0,0 +1,18 @@
1
+ export const HEX_DIGIT_MAP = {
2
+ 0: 0x200b,
3
+ 1: 0x200c,
4
+ 2: 0x200d,
5
+ 3: 0x2062,
6
+ 4: 0x2063,
7
+ 5: 0x2060,
8
+ 6: 0xfeff,
9
+ 7: 0x2061,
10
+ 8: 0x1d173,
11
+ 9: 0x1d174,
12
+ a: 0x1d175,
13
+ b: 0x1d176,
14
+ c: 0x1d177,
15
+ d: 0x1d178,
16
+ e: 0x1d179,
17
+ f: 0x1d17a,
18
+ };
@@ -0,0 +1,2 @@
1
+ export * from './encode';
2
+ export * from './decode';
package/dist/map.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ export declare const HEX_DIGIT_MAP: {
2
+ 0: number;
3
+ 1: number;
4
+ 2: number;
5
+ 3: number;
6
+ 4: number;
7
+ 5: number;
8
+ 6: number;
9
+ 7: number;
10
+ 8: number;
11
+ 9: number;
12
+ a: number;
13
+ b: number;
14
+ c: number;
15
+ d: number;
16
+ e: number;
17
+ f: number;
18
+ };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@vercel/stega",
3
+ "version": "0.0.1",
4
+ "description": "Utilities for steganography",
5
+ "main": "./dist/cjs/index.js",
6
+ "module": "./dist/esm/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "source": "./src/index.ts",
9
+ "files": ["dist"],
10
+ "scripts": {
11
+ "dev": "npm run build",
12
+ "build": "npm run build:types && npm run build:esm && npm run build:cjs",
13
+ "build:esm": "tsc --module es2020 --outDir dist/esm",
14
+ "build:cjs": "tsc --module commonjs --outDir dist/cjs",
15
+ "build:types": "tsc --declaration --emitDeclarationOnly",
16
+ "test": "jest"
17
+ },
18
+ "devDependencies": {
19
+ "@jest/globals": "^28.1.3",
20
+ "jest": "^28.1.3",
21
+ "ts-jest": "^28.0.3",
22
+ "typescript": "^4.6.3"
23
+ }
24
+ }