cdn

package
v0.0.0-...-8142744 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cdn provides CDN utilities for WeChat media transfer. This is a complete Go reimplementation of the TypeScript cdn module.

Package cdn provides CDN download utilities. This is a complete Go reimplementation of the TypeScript pic-decrypt.ts module.

Package cdn provides CDN upload utilities. This is a complete Go reimplementation of the TypeScript upload.ts module.

Index

Constants

View Source
const (
	UploadMaxRetries  = 3
	DefaultCDNBaseURL = "https://novac2c.cdn.weixin.qq.com/c2c"
)

Variables

This section is empty.

Functions

func AES128ECBPaddedSize

func AES128ECBPaddedSize(plaintextSize int) int

AES128ECBPaddedSize returns the ciphertext size for given plaintext size.

func DecryptAES128ECB

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

DecryptAES128ECB decrypts ciphertext using AES-128-ECB with PKCS7 padding.

func EncryptAES128ECB

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

EncryptAES128ECB encrypts plaintext using AES-128-ECB with PKCS7 padding.

func GenerateAESKey

func GenerateAESKey() ([]byte, error)

GenerateAESKey generates a random 16-byte AES key.

func GenerateFileKey

func GenerateFileKey() (string, error)

GenerateFileKey generates a random 16-byte file key (hex-encoded).

func MD5Hash

func MD5Hash(data []byte) string

MD5Hash computes the MD5 hash of data and returns hex string.

Types

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

Downloader handles CDN downloads with AES-128-ECB decryption.

func NewDownloader

func NewDownloader(cdnBaseURL string) *Downloader

NewDownloader creates a new downloader.

func (*Downloader) DownloadAndDecrypt

func (d *Downloader) DownloadAndDecrypt(ctx context.Context, encryptedQueryParam, aesKeyBase64 string) ([]byte, error)

DownloadAndDecrypt downloads and decrypts a file from CDN.

func (*Downloader) DownloadImage

func (d *Downloader) DownloadImage(ctx context.Context, encryptedQueryParam, aesKeyHex string) ([]byte, error)

DownloadImage downloads and decrypts an image from CDN. The aesKeyHex is the hex-encoded AES key from ImageItem.AESKey.

func (*Downloader) DownloadMedia

func (d *Downloader) DownloadMedia(ctx context.Context, encryptedQueryParam, aesKey string) ([]byte, error)

DownloadMedia downloads and decrypts any media type from CDN. Uses the appropriate key format based on the key string format.

func (*Downloader) DownloadPlain

func (d *Downloader) DownloadPlain(ctx context.Context, encryptedQueryParam string) ([]byte, error)

DownloadPlain downloads unencrypted data from CDN.

type ECBEncryptor

type ECBEncryptor struct {
	// contains filtered or unexported fields
}

ECBEncryptor provides AES-128-ECB encryption/decryption.

func NewECBEncryptor

func NewECBEncryptor(key []byte) *ECBEncryptor

NewECBEncryptor creates a new ECB encryptor.

func (*ECBEncryptor) Decrypt

func (e *ECBEncryptor) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt decrypts ciphertext.

func (*ECBEncryptor) Encrypt

func (e *ECBEncryptor) Encrypt(plaintext []byte) ([]byte, error)

Encrypt encrypts plaintext.

func (*ECBEncryptor) Key

func (e *ECBEncryptor) Key() []byte

Key returns the encryption key.

func (*ECBEncryptor) KeyHex

func (e *ECBEncryptor) KeyHex() string

KeyHex returns the hex-encoded key.

type UploadedFileInfo

type UploadedFileInfo struct {
	FileKey                     string
	DownloadEncryptedQueryParam string
	AESKey                      string // hex-encoded
	FileSize                    int    // plaintext size
	FileSizeCiphertext          int    // ciphertext size after encryption
}

UploadedFileInfo represents information about an uploaded file.

type Uploader

type Uploader struct {
	// contains filtered or unexported fields
}

Uploader handles CDN uploads with AES-128-ECB encryption.

func NewUploader

func NewUploader(client *api.Client, cdnBaseURL string) *Uploader

NewUploader creates a new uploader.

func (*Uploader) UploadFile

func (u *Uploader) UploadFile(ctx context.Context, plaintext []byte, toUserID string, mediaType api.UploadMediaType) (*UploadedFileInfo, error)

UploadFile uploads a file to CDN and returns the file info. This implements the complete upload flow: 1. Calculate plaintext size and MD5 2. Generate AES key 3. Calculate ciphertext size 4. Get upload URL from API 5. Encrypt and upload to CDN

func (*Uploader) UploadFileAttachment

func (u *Uploader) UploadFileAttachment(ctx context.Context, fileData []byte, toUserID string) (*UploadedFileInfo, error)

UploadFileAttachment uploads a file attachment to CDN.

func (*Uploader) UploadImage

func (u *Uploader) UploadImage(ctx context.Context, imageData []byte, toUserID string) (*UploadedFileInfo, error)

UploadImage uploads an image to CDN.

func (*Uploader) UploadVideo

func (u *Uploader) UploadVideo(ctx context.Context, videoData []byte, toUserID string) (*UploadedFileInfo, error)

UploadVideo uploads a video to CDN.

Jump to

Keyboard shortcuts

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