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
- func AES128ECBPaddedSize(plaintextSize int) int
- func DecryptAES128ECB(ciphertext, key []byte) ([]byte, error)
- func EncryptAES128ECB(plaintext, key []byte) ([]byte, error)
- func GenerateAESKey() ([]byte, error)
- func GenerateFileKey() (string, error)
- func MD5Hash(data []byte) string
- type Downloader
- func (d *Downloader) DownloadAndDecrypt(ctx context.Context, encryptedQueryParam, aesKeyBase64 string) ([]byte, error)
- func (d *Downloader) DownloadImage(ctx context.Context, encryptedQueryParam, aesKeyHex string) ([]byte, error)
- func (d *Downloader) DownloadMedia(ctx context.Context, encryptedQueryParam, aesKey string) ([]byte, error)
- func (d *Downloader) DownloadPlain(ctx context.Context, encryptedQueryParam string) ([]byte, error)
- type ECBEncryptor
- type UploadedFileInfo
- type Uploader
- func (u *Uploader) UploadFile(ctx context.Context, plaintext []byte, toUserID string, ...) (*UploadedFileInfo, error)
- func (u *Uploader) UploadFileAttachment(ctx context.Context, fileData []byte, toUserID string) (*UploadedFileInfo, error)
- func (u *Uploader) UploadImage(ctx context.Context, imageData []byte, toUserID string) (*UploadedFileInfo, error)
- func (u *Uploader) UploadVideo(ctx context.Context, videoData []byte, toUserID string) (*UploadedFileInfo, error)
Constants ¶
const ( UploadMaxRetries = 3 DefaultCDNBaseURL = "https://novac2c.cdn.weixin.qq.com/c2c" )
Variables ¶
This section is empty.
Functions ¶
func AES128ECBPaddedSize ¶
AES128ECBPaddedSize returns the ciphertext size for given plaintext size.
func DecryptAES128ECB ¶
DecryptAES128ECB decrypts ciphertext using AES-128-ECB with PKCS7 padding.
func EncryptAES128ECB ¶
EncryptAES128ECB encrypts plaintext using AES-128-ECB with PKCS7 padding.
func GenerateAESKey ¶
GenerateAESKey generates a random 16-byte AES key.
func GenerateFileKey ¶
GenerateFileKey generates a random 16-byte file key (hex-encoded).
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 ¶
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) 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 ¶
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.