Metadata-Version: 2.5
Name: tiktok-forge
Version: 1.1.0
Summary: TikTok signature forge: real web (X-Bogus via the vendor SDK) and app (Argus/Gorgon/Ladon/TTEncrypt) signatures
Project-URL: Homepage, https://github.com/6x-u/tiktok-forge
Project-URL: Repository, https://github.com/6x-u/tiktok-forge
Project-URL: Issues, https://github.com/6x-u/tiktok-forge/issues
Project-URL: Changelog, https://github.com/6x-u/tiktok-forge/releases
Author: MERO
License: MIT
License-File: LICENSE
Keywords: android,api,authentication,mstoken,signature,tiktok,ttencrypt,ttwid,verifyfp,webmssdk,x-argus,x-bogus,x-gorgon,x-khronos,x-ladon
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet
Classifier: Topic :: Security :: Cryptography
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Requires-Dist: pycryptodome>=3.20.0
Requires-Dist: requests>=2.28.0
Provides-Extra: dev
Requires-Dist: build>=1.2.0; extra == 'dev'
Requires-Dist: mypy>=1.10.0; extra == 'dev'
Requires-Dist: pytest-cov>=5.0.0; extra == 'dev'
Requires-Dist: pytest>=8.0.0; extra == 'dev'
Requires-Dist: ruff>=0.4.0; extra == 'dev'
Requires-Dist: twine>=5.0.0; extra == 'dev'
Description-Content-Type: text/markdown

# tiktok-forge

[![PyPI](https://img.shields.io/pypi/v/tiktok-forge?style=flat-square&label=pypi&color=blue)](https://pypi.org/project/tiktok-forge/)
[![Python](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-3776AB?style=flat-square&logo=python&logoColor=white)](https://pypi.org/project/tiktok-forge/)
[![Node](https://img.shields.io/badge/node-%E2%89%A516-339933?style=flat-square&logo=nodedotjs&logoColor=white)](https://nodejs.org)
[![License](https://img.shields.io/badge/license-MIT-green?style=flat-square)](https://github.com/6x-u/tiktok-forge/blob/main/LICENSE)
[![Tests](https://img.shields.io/badge/tests-40%20passed-brightgreen?style=flat-square)](https://github.com/6x-u/tiktok-forge)
[![webmssdk](https://img.shields.io/badge/webmssdk-1.0.0.417-orange?style=flat-square)](https://github.com/6x-u/tiktok-forge)
[![app](https://img.shields.io/badge/app-47.5.3-000000?style=flat-square&logo=tiktok&logoColor=white)](https://github.com/6x-u/tiktok-forge)

[![GitHub](https://img.shields.io/badge/github-6x--u%2Ftiktok--forge-181717?style=flat-square&logo=github&logoColor=white)](https://github.com/6x-u/tiktok-forge)
[![Telegram](https://img.shields.io/badge/telegram-%40QP4RM-26A5E4?style=flat-square&logo=telegram&logoColor=white)](https://t.me/QP4RM)

TikTok request signing for **web** and **app** — one import, real values on the wire.

```python
from tiktokforge import WebClient, TikTokSigner

with WebClient() as c:
    c.get("/api/challenge/detail/", params={"challengeName": "fyp"}).json()

TikTokSigner.generate_headers(params="aid=1233&device_id=7171234567890123456")
```

---

## Install

```bash
pip install tiktok-forge
```

| Requirement | Why |
|---|---|
| Python 3.10+ | dataclasses, typing |
| Node 16+ | executes the vendored web bundle |
| `pycryptodome`, `requests` | pulled in automatically |

Node is resolved from `TIKTOKFORGE_NODE`, then `PATH`, then the usual install
directories. If it isn't found the bridge raises `NodeUnavailable` and lists
every path it tried. **App signing works with no node installed.**

---

## Why this exists

Signatures rot. Every app release re-rolls the identity, every web build re-rolls
the bundle, and most packages on this shelf are a frozen table someone scraped in
2023. They keep returning strings. The strings stop working.

The split here is boring on purpose:

- **Web** — `X-Bogus` is produced by **TikTok's own bundle**, running. We ship
  `webmssdk.js`, boot it in a browser shim, and call the same entry point the
  site calls. No hand-ported cipher, no table guessing.
- **App** — `x-argus`, `x-gorgon`, `x-ladon` are arithmetic, so they live in
  Python. The identity they're keyed with (`version_name`, `version_code`,
  `sdk_ver`, device profile) sits in one place instead of being sprinkled
  through crypto code.

Rules that came from things breaking:

1. **If it isn't running their code, it isn't a signature.** A string that looks
   right and 403s is worse than nothing — you build a pipeline on top of it first.
2. **No invented headers.** If the bundle doesn't emit `X-Gnarly`, we don't emit
   `X-Gnarly`.
3. **Version identity is config, not code.** Moving to a new app build is editing
   a dataclass.
4. **Prove it, then ship it.** The updater boots a candidate bundle, signs a real
   URL, prints the result, and only then replaces the file.
5. **Fuck is what you get when you reimplement a rotating cipher from a blog
   post.** Read the tables once and stop guessing where the salt goes.
6. **Docs lie, tests don't.** 40 offline tests, plus a CLI that hits the real edge.

---

## Web

```python
from tiktokforge import WebClient

with WebClient(retries=2, timeout=25) as client:
    r = client.get("/api/challenge/detail/", params={"challengeName": "fyp"})
    print(r.status_code, r.json()["challengeInfo"]["challenge"]["id"])
```

`WebClient` is a `requests` session that signs every call fresh, merges the
standard `aid=1988` parameter set, and retries when the edge answers `200` with an
empty body — that's what a stale signature looks like from outside.

Lower level:

```python
from tiktokforge import sign_web, with_signature, generate_web_headers

sign_web("https://www.tiktok.com/api/post/item_list/?aid=1988&count=20")
# {'X-Bogus': 'WMu77aEMZKcMsdoF'}

with_signature("https://www.tiktok.com/api/post/item_list/?aid=1988")
# '...&X-Bogus=WMu77aEMZKcMsdoF'

generate_web_headers("https://www.tiktok.com/api/challenge/detail/?aid=1988")
# User-Agent, Referer, X-Bogus, Cookie(msToken + tt_webid + s_v_web_id)
```

One long-lived engine, if you're signing in a loop:

```python
from tiktokforge import get_bridge

engine = get_bridge()      # boots once
engine.sign("https://www.tiktok.com/api/post/item_list/?aid=1988")
engine.close()
```

Measured here: **100 signatures in 108 ms — 1.08 ms each** after boot. Spawning
node per signature costs ~250 ms.

Produced values:

| Value | Source |
|---|---|
| `X-Bogus` | vendored bundle, 16 chars, time-seeded |
| `msToken` | 107-char urlsafe base64 |
| `ttwid` | `ttwid.bytedance.com/ttwid/union/register/` |
| `verifyFp` / `s_v_web_id` | device fingerprint |
| `tt_webid` / `webId` | 19-digit id |

No `X-Gnarly` — the current bundle has no such output.

---

## App

```python
from tiktokforge import APP_DEFAULTS, TikTokSigner

params = (
    "aid=1233&app_name=musical_ly&device_platform=android"
    "&device_id=7171234567890123456&version_name=47.5.3&os_version=15"
)
headers = TikTokSigner.generate_headers(params=params, data="username=demo")

# x-ss-req-ticket  x-ss-stub  x-tt-trace-id
# x-khronos        x-ladon    x-gorgon      x-argus
```

Send with your own client:

```python
import requests

url = f"https://api.tiktokv.com/aweme/v1/feed/?{params}"
r = requests.get(url, headers={"User-Agent": APP_DEFAULTS.user_agent(), **headers})
```

### Pinned device identity

```python
from tiktokforge import AppDefaults, DeviceProfile, TikTokSigner

profile = AppDefaults(device=DeviceProfile(
    device_id="7171234567890123456",
    install_id="7171234567890123457",
    device_type="SM-S928B",
    device_brand="samsung",
    os_version="15",
    os_api=35,
))
profile.user_agent()
# com.zhiliaoapp.musically/2023705030 (Linux; U; Android 15; en_US; SM-S928B; ...)

TikTokSigner.generate_headers(params=params, profile=profile)
```

The UA and the signatures come from the same profile, so they can't disagree.

### Version handling

```python
from tiktokforge import version_code_for

version_code_for("47.5.3")   # 2023705030
```

Derivation: `2023` + `major%10` + `minor:02` + `patch:02` + `0`. Deviating
releases go in `VERSION_CODE_OVERRIDES` inside `tiktokforge/config.py`.
Per-call override: `TikTokSigner.generate_headers(..., version_code=2023705030)`.

Current defaults: app **47.5.3**, sdk **v05.01.03-ov-android**, aid **1233**.

### TTEncrypt

The app gzips the protobuf body, then encrypts it. Same here by default:

```python
blob = TikTokSigner.encrypt({"device_id": "7171234567890123456", "aid": 1233})
TikTokSigner.decrypt(blob)   # -> '{"device_id":"7171234567890123456","aid":1233}'
```

`encrypt(..., compress=False)` for raw blobs.

---

## Verify the install

```bash
tiktokforge-verify
```

```
library      1.1.0
node         C:\Program Files\nodejs\node.exe
bundle       webmssdk.js
X-Bogus      WDAK7QqovEy/i+Zg
cookies      msToken, s_v_web_id, tt_webid, tt_webid_v2
status       200  (727 bytes)
result       OK
```

Flags: `--json`, `--no-net`, `--path`, `--params`.

---

## API surface

| Call | Returns |
|---|---|
| `TikTokSigner.generate_headers(params, data=..., profile=...)` | `dict` of `x-*` headers |
| `TikTokSigner.encrypt(obj)` / `.decrypt(blob)` | TTEncrypt round-trip |
| `sign_web(url)` | `{'X-Bogus': ...}` |
| `with_signature(url)` | URL with `X-Bogus` appended |
| `generate_web_headers(url)` | full header set with cookies |
| `WebClient().get/post(...)` | signed response |
| `generate_ms_token()` / `generate_verify_fp()` / `generate_web_id()` | tokens |
| `get_bridge()` | long-lived signing engine |
| `version_code_for("47.5.3")` | `2023705030` |

`tiktokforge.algorithms` exposes the primitives: `Simon`, `SM3`, `Argus`,
`Gorgon`, `Ladon`, `TTEncrypt`, `ProtoBuf`.

---

## Notes

- The web signer needs node; the app signer does not.
- The vendored bundle is refreshed from a source checkout with
  `python tools/update_bundle.py` — the wheel ships the one it was built with.
- Tests: 40 offline. Live checks go through `tiktokforge-verify`, so a network
  hiccup never shows up as a red test.

---

## License

MIT.

Built by MERO — **Telegram [@QP4RM](https://t.me/QP4RM)** · source
[github.com/6x-u/tiktok-forge](https://github.com/6x-u/tiktok-forge)
