Skip to main content

TikTok Android API authentication signature generator

Project description

TikTok Signer

Python library for generating TikTok Android API authentication signatures. This library implements the required encryption algorithms (Argus, Gorgon, Ladon, TTEncrypt) for authenticating requests to TikTok's private API.

Installation

From pip

pip install git+https://github.com/iqbalmh18/tiktok-signer.git

From Source

git clone https://github.com/iqbalmh18/tiktok-signer.git
cd tiktok-signer
pip install .

Development

git clone https://github.com/iqbalmh18/tiktok-signer.git
cd tiktok-signer
pip install -e ".[dev]"

Requirements

  • Python >= 3.10
  • pycryptodome >= 3.20.0

Quick Start

from tiktok_signer import TikTokSigner

# Generate authentication headers
headers = TikTokSigner.generate_headers(params="aid=1233&app_name=musical_ly")
print(headers)

# Encrypt device registration payload
encrypted = TikTokSigner.encrypt({"device_id": "123456", "os": "android"})

# Decrypt response
decrypted = TikTokSigner.decrypt(encrypted_bytes)

API Reference

TikTokSigner

Main class for signature generation and encryption.

TikTokSigner.generate_headers()

Generates all authentication headers required for TikTok API requests.

headers = TikTokSigner.generate_headers(
    params,                                      # Required: URL query parameters (str or dict)
    data=None,                                   # Optional: Request body for POST (str, bytes, or dict)
    device_id="",                                # Optional: Device identifier
    aid=1233,                                    # Optional: Application ID (int or str)
    lc_id=2142840551,                            # Optional: License ID (int or str)
    sdk_ver="v05.01.02-alpha.7-ov-android",      # Optional: SDK version string
    sdk_ver_code=83952160,                       # Optional: SDK version code (int or str)
    app_ver="37.0.4",                            # Optional: App version string
    version_code=2023700040,                         # Optional: App version code (int or str)
    cookie=None,                                 # Optional: Cookie string
    unix=None                                    # Optional: Unix timestamp in seconds
)

Parameters:

Parameter Type Default Description
params str or dict Required URL query parameters
data str, bytes, dict, or None None Request body for POST requests
device_id str "" Device identifier
aid int or str 1233 Application ID
lc_id int or str 2142840551 License ID
sdk_ver str "v05.01.02-alpha.7-ov-android" SDK version string
sdk_ver_code int or str 83952160 SDK version code
app_ver str "37.0.4" App version string
version_code int or str 2023700040 App version code
cookie str or None None Cookie string
unix int or None None Unix timestamp in seconds. If None, uses current time.

Returns: Dict[str, str]

{
    "x-ss-req-ticket": "1706789012345",      # Request timestamp (milliseconds)
    "x-tt-trace-id": "00-abc123...-01",       # Trace identifier
    "x-ss-stub": "ABC123...",                 # Body hash (only if data is provided)
    "x-ladon": "base64_encoded_token",        # Ladon authentication
    "x-gorgon": "8404...",                    # Gorgon signature
    "x-khronos": "1706789012",                # Unix timestamp
    "x-argus": "base64_encoded_token",        # Argus authentication
    "cookie": "..."                           # Cookie (only if provided)
}

TikTokSigner.encrypt()

Encrypts data using the TTEncrypt algorithm for device registration.

encrypted = TikTokSigner.encrypt(data)

Parameters:

Parameter Type Description
data str, bytes, or dict Data to encrypt. Dict will be converted to JSON.

Returns: bytes - Encrypted data

TikTokSigner.decrypt()

Decrypts data encrypted with TTEncrypt.

decrypted = TikTokSigner.decrypt(encrypted_bytes)

Parameters:

Parameter Type Description
data bytes Encrypted data

Returns: str - Decrypted data (typically a JSON string)

Shortcut Functions

The library also provides shortcut functions for convenience:

from tiktok_signer import generate_headers, encrypt, decrypt

# Same as TikTokSigner.generate_headers()
headers = generate_headers(params="aid=1233")

# Same as TikTokSigner.encrypt()
encrypted = encrypt({"key": "value"})

# Same as TikTokSigner.decrypt()
decrypted = decrypt(encrypted_bytes)

Usage Examples

GET Request

import requests
from tiktok_signer import TikTokSigner

# Define parameters
params = {
    "aid": 1233,
    "app_name": "musical_ly",
    "device_platform": "android",
    "os_version": "9",
    "device_type": "2203121C",
    "device_brand": "Xiaomi",
    "language": "id",
    "region": "ID",
}

# Convert to query string
query_string = "&".join(f"{k}={v}" for k, v in params.items())

# Generate authentication headers
auth_headers = TikTokSigner.generate_headers(params=query_string)

# Base headers
headers = {
    "User-Agent": "com.zhiliaoapp.musically/2023700040 (Linux; U; Android 9; in_ID; 2203121C; Build/PQ3A.190705.09121607;tt-ok/3.12.13.4-tiktok)",
    "Connection": "Keep-Alive",
    "Accept-Encoding": "gzip",
}
headers.update(auth_headers)

# Send request
url = f"https://api.tiktokv.com/aweme/v1/feed/?{query_string}"
response = requests.get(url, headers=headers)
print(response.json())

POST Request with Body

import requests
from tiktok_signer import TikTokSigner

params = "aid=1233&app_name=musical_ly"

# Request body
data = {
    "username": "example",
    "password": "encrypted_password",
    "mix_mode": 1,
}

# Generate headers with data body
auth_headers = TikTokSigner.generate_headers(
    params=params,
    data=data,
    cookie="sessionid=abc123; tt_csrf_token=xyz789"
)

headers = {
    "User-Agent": "com.zhiliaoapp.musically/2023700040 (Linux; U; Android 9; in_ID; 2203121C)",
    "Content-Type": "application/x-www-form-urlencoded",
}
headers.update(auth_headers)

url = f"https://api.tiktokv.com/passport/user/login/?{params}"
response = requests.post(url, headers=headers, data=data)
print(response.json())

Using Custom Unix Timestamp

import time
from tiktok_signer import TikTokSigner

# Use custom unix timestamp (useful for replay or testing)
custom_unix = int(time.time()) - 60  # 1 minute ago

headers = TikTokSigner.generate_headers(
    params="aid=1233&app_name=musical_ly",
    unix=custom_unix
)

# x-khronos will reflect the custom timestamp
print(f"x-khronos: {headers['x-khronos']}")  # Will show custom_unix value

Device Registration with TTEncrypt

import requests
from tiktok_signer import TikTokSigner

# Device info payload
device_info = {
    "magic_tag": "ss_app_log",
    "header": {
        "display_name": "TikTok",
        "update_version_code": 2023700040,
        "manifest_version_code": 2023700040,
        "app_version_minor": "",
        "aid": 1233,
        "channel": "googleplay",
        "package": "com.zhiliaoapp.musically",
        "app_version": "37.0.4",
        "version_code": 2023700040,
        "sdk_ver_code": "3.9.17-bugfix.9",
        "os": "Android",
        "os_version": "9",
        "os_api": 28,
        "device_model": "2203121C",
        "device_brand": "Xiaomi",
        "device_manufacturer": "Xiaomi",
        "cpu_abi": "arm64-v8a",
        "release_build": "7e6048c_20231219",
        "density_dpi": 320,
        "display_density": "mdpi",
        "resolution": "720*1280",
        "language": "id",
        "timezone": 7,
        "access": "wifi",
        "not_request_sender": 0,
        "rom": "MIUI-V12.5.6.0.QDLMIXM",
        "rom_version": "miui_V12_V12.5.6.0.QDLMIXM",
        "sig_hash": "194326e82c84a639a52e5c023116f12a",
        "google_aid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "openudid": "xxxxxxxxxxxxxxxx",
        "clientudid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    },
    "_gen_time": 1706789012345,
}

# Encrypt payload
encrypted_payload = TikTokSigner.encrypt(device_info)

# Send encrypted request
params = "aid=1233&app_name=musical_ly"
auth_headers = TikTokSigner.generate_headers(params=params, data=encrypted_payload)

headers = {
    "User-Agent": "com.zhiliaoapp.musically/2023700040 (Linux; U; Android 9; in_ID; 2203121C)",
    "Content-Type": "application/octet-stream",
}
headers.update(auth_headers)

url = f"https://log.tiktokv.com/service/2/device_register/?{params}"
response = requests.post(url, headers=headers, data=encrypted_payload)
print(response.json())

Using Dict for Parameters

from tiktok_signer import TikTokSigner

# Parameters can be a dict
params = {
    "aid": 1233,
    "app_name": "musical_ly",
    "device_platform": "android",
}

# Library will automatically convert to query string
headers = TikTokSigner.generate_headers(params=params)

Custom App and SDK Versions

from tiktok_signer import TikTokSigner

headers = TikTokSigner.generate_headers(
    params="aid=1233&app_name=musical_ly",
    sdk_ver="v05.01.02-alpha.7-ov-android",  # SDK version
    sdk_ver_code=83952160,                    # SDK version code
    app_ver="38.0.0",                         # App version
    version_code=380000                       # App version code
)

Encryption Algorithms

This library implements 4 main encryption algorithms:

Algorithm Header Output Description
Argus x-argus Primary signature using Simon cipher and SM3 hash
Gorgon x-gorgon, x-khronos Request integrity signature with timestamp
Ladon x-ladon License-based authentication token
TTEncrypt - Payload encryption for device registration

Internal Components

Module Description
protobuf.py Protocol Buffer encoding/decoding for Argus payload
simon.py Simon 128/256 block cipher implementation
sm3.py SM3 cryptographic hash (Chinese national standard GB/T 32905-2016)
stub.py MD5 stub generation for request body

Default Values

The library uses the following default values based on the TikTok Android app:

Parameter Default Value Description
aid 1233 Application ID
lc_id 2142840551 License ID
sdk_ver "v05.01.02-alpha.7-ov-android" SDK version string
sdk_ver_code 83952160 SDK version code
app_ver "37.0.4" App version string
version_code 2023700040 App version code

Project Structure

tiktok-signer/
├── pyproject.toml              # Project configuration
├── README.md                   # Documentation
├── LICENSE                     # MIT License
├── MANIFEST.in                 # Package manifest
└── tiktok_signer/              # Main package
    ├── __init__.py             # Package exports
    ├── signer.py               # TikTokSigner class
    ├── example.py              # Usage examples
    └── lib/                    # Internal library
        ├── __init__.py
        ├── argus.py            # X-Argus encryption
        ├── gorgon.py           # X-Gorgon encryption
        ├── ladon.py            # X-Ladon encryption
        ├── stub.py             # Body hash generation
        ├── ttencrypt.py        # Device payload encryption
        ├── data/
        │   └── dword.json      # Lookup tables
        └── utils/
            ├── __init__.py
            ├── protobuf.py     # Protocol Buffer
            ├── simon.py        # Simon cipher
            └── sm3.py          # SM3 hash

Error Handling

from tiktok_signer import TikTokSigner

try:
    headers = TikTokSigner.generate_headers(params=params)
except Exception as e:
    print(f"Signature generation failed: {e}")

Type Annotations

All public functions include complete type annotations:

def generate_headers(
    params: Union[str, Dict],
    data: Optional[Union[str, bytes, Dict]] = None,
    device_id: str = "",
    aid: Union[int, str] = 1233,
    lc_id: Union[int, str] = 2142840551,
    sdk_ver: str = "v05.01.02-alpha.7-ov-android",
    sdk_ver_code: Union[int, str] = 83952160,
    app_ver: str = "37.0.4",
    version_code: Union[int, str] = 2023700040,
    cookie: Optional[str] = None,
    unix: Optional[int] = None
) -> Dict[str, str]:
    ...

Changelog

v1.1.0

  • Consolidated signer into single robust module
  • Added unix parameter for custom timestamp support
  • Added app_ver and version_code parameters
  • Added default value from TikTok App (2026)
  • Separated SDK version (sdk_ver, sdk_ver_code) from App version (app_ver, version_code)

v1.0.0

  • Initial release
  • Implement Argus, Gorgon, Ladon encryption
  • Implement TTEncrypt for device registration
  • Full type annotations

License

MIT License. See LICENSE file for details.

Disclaimer

This library is provided for educational and research purposes only. Use of this library must comply with TikTok's Terms of Service and applicable laws. The author is not responsible for any misuse of this library.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tiktok_signer-1.1.0.tar.gz (41.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tiktok_signer-1.1.0-py3-none-any.whl (45.7 kB view details)

Uploaded Python 3

File details

Details for the file tiktok_signer-1.1.0.tar.gz.

File metadata

  • Download URL: tiktok_signer-1.1.0.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for tiktok_signer-1.1.0.tar.gz
Algorithm Hash digest
SHA256 83f5ee683b785718cc8ba03f23d541d8cb4dcb66f39583edf65a776f523da36c
MD5 d60217a7907e39edd541756c8a740270
BLAKE2b-256 6978489904986d18d8b04d1792e22a08b19f6fe8cb44eba7f014e367dcf52f46

See more details on using hashes here.

File details

Details for the file tiktok_signer-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: tiktok_signer-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 45.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for tiktok_signer-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abc0f4a663ab3e12f2100ee37aaaa9881130cf963a89bd19918c0ee3352cf51b
MD5 c075fef84d28dab2031847e143aaa56c
BLAKE2b-256 190de6d54829ddbc43c821e67cb973ffeb550a2d2401392c58d8602795c6eefb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page