Help us shape the future of Go! Take the 2026 Go Developer Survey

cdn

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package cdn talks to the WeChat C2C CDN: encrypts files locally with AES-128-ECB, hands them to the upload endpoint, and inverts the flow on the download side. It exists as its own package so the listener layer doesn't have to carry the iLink-specific crypto helpers, and so tests can target the CDN protocol without spinning up the full long-poll listener.

Index

Constants

This section is empty.

Variables

View Source
var BaseURL = "https://novac2c.cdn.weixin.qq.com/c2c"

BaseURL is declared as a var (not const) so tests can override it with an httptest.Server. In production it stays pinned to the live CDN.

Functions

func Decrypt

func Decrypt(ciphertext, key []byte) ([]byte, error)

Decrypt decrypts AES-128-ECB data and strips PKCS7 padding.

SECURITY: ECB mode is insecure for general-purpose encryption because identical plaintext blocks produce identical ciphertext blocks. This helper exists only for WeChat iLink CDN protocol compatibility. Do not reuse it for local storage, tokens, secrets, or user data encryption.

func Download

func Download(ctx context.Context, encryptQueryParam, aesKeyBase64 string) ([]byte, error)

Download downloads and decrypts a file from the WeChat CDN.

func Encrypt

func Encrypt(plaintext, key []byte) ([]byte, error)

Encrypt encrypts data using AES-128-ECB with PKCS7 padding.

SECURITY: ECB mode is insecure for general-purpose encryption because identical plaintext blocks produce identical ciphertext blocks. This helper exists only for WeChat iLink CDN protocol compatibility. Do not reuse it for local storage, tokens, secrets, or user data encryption.

func HexDecodedAsBase64

func HexDecodedAsBase64(hexKey string) string

HexDecodedAsBase64 hex-decodes the key first, then base64-encodes the raw 16 bytes — i.e. hex → 16 bytes → base64. This is the encoding iLink uses for inbound image AES keys (per ParseAESKey's 16-byte branch).

Falls back to the HexStringAsBase64 encoding when the input isn't valid hex, matching upstream behaviour: some legacy payloads put the base64 form directly in the hex field.

func HexStringAsBase64

func HexStringAsBase64(hexKey string) string

HexStringAsBase64 treats the hex key's ASCII characters as opaque bytes and base64-encodes them — i.e. base64("abcdef012345...") → "YWJjZGVmMDEyMzQ1..." so the receiver will base64-decode back to the hex string, then hex-decode to the 16-byte key. This is the encoding iLink expects for outbound file/voice/video AES keys (per ParseAESKey's 32-char-hex branch).

Pair with HexDecodedAsBase64 below; the names spell out the difference because the older `aesKeyToBase64` form hid the "treat-as-string" semantics.

func PaddedSize

func PaddedSize(plaintextSize int) int

PaddedSize returns the size of the AES-128-ECB ciphertext for a plaintext of the supplied length, including the mandatory PKCS7 padding block.

func ParseAESKey

func ParseAESKey(aesKeyBase64 string) ([]byte, error)

ParseAESKey decodes a base64-encoded AES key. Two formats:

  • base64(raw 16 bytes) → images (aes_key from media field)
  • base64(32-char hex string) → file / voice / video

Types

type UploadedFile

type UploadedFile struct {
	DownloadParam string // encrypted query param for download
	AESKeyHex     string // hex-encoded AES key
	FileSize      int    // plaintext size
	CipherSize    int    // ciphertext size
}

UploadedFile holds the result of a CDN upload.

func Upload

func Upload(ctx context.Context, client *ilink.Client, data []byte, toUserID string, mediaType int) (*UploadedFile, error)

Upload encrypts and uploads a file to the WeChat CDN.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL