|
11精币
[color=var(--color-prettylights-syntax-keyword)]import hmac[color=var(--color-prettylights-syntax-keyword)]import hashlib[color=var(--color-prettylights-syntax-keyword)]import requests[color=var(--color-prettylights-syntax-keyword)]import time[color=var(--color-prettylights-syntax-keyword)]def [color=var(--color-prettylights-syntax-entity)]hmac_sha256(key, message): [color=var(--color-prettylights-syntax-string)]"""[color=var(--color-prettylights-syntax-string)] 使用HMAC-SHA256算法对给定的消息进行加密[color=var(--color-prettylights-syntax-string)] :param key: 密钥[color=var(--color-prettylights-syntax-string)] :param message: 要加密的消息[color=var(--color-prettylights-syntax-string)] :return: 加密后的哈希值[color=var(--color-prettylights-syntax-string)] """ [color=var(--color-prettylights-syntax-comment)]# 将密钥和消息转换为字节串 key [color=var(--color-prettylights-syntax-constant)]= key.[color=var(--color-prettylights-syntax-entity)]encode([color=var(--color-prettylights-syntax-string)]'utf-8') message [color=var(--color-prettylights-syntax-constant)]= message.[color=var(--color-prettylights-syntax-entity)]encode([color=var(--color-prettylights-syntax-string)]'utf-8') [color=var(--color-prettylights-syntax-comment)]# 创建HMAC对象,使用SHA256哈希算法 hmac_obj [color=var(--color-prettylights-syntax-constant)]= hmac.[color=var(--color-prettylights-syntax-entity)]new(key, message, hashlib.sha256) [color=var(--color-prettylights-syntax-comment)]# 计算哈希值 hash_value [color=var(--color-prettylights-syntax-constant)]= hmac_obj.[color=var(--color-prettylights-syntax-entity)]digest() [color=var(--color-prettylights-syntax-comment)]# 将哈希值转换为十六进制字符串 hash_hex [color=var(--color-prettylights-syntax-constant)]= hash_value.[color=var(--color-prettylights-syntax-entity)]hex() [color=var(--color-prettylights-syntax-keyword)]return hash_hex[color=var(--color-prettylights-syntax-keyword)]if __name__ [color=var(--color-prettylights-syntax-constant)]== [color=var(--color-prettylights-syntax-string)]'__main__': o [color=var(--color-prettylights-syntax-constant)]= [color=var(--color-prettylights-syntax-entity)]hmac_sha256([color=var(--color-prettylights-syntax-string)]"XgwSnGZ1p",[color=var(--color-prettylights-syntax-string)]f"ts{int(time.time())}") url [color=var(--color-prettylights-syntax-constant)]= [color=var(--color-prettylights-syntax-string)]"https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket" [color=var(--color-prettylights-syntax-entity)]print(o) params [color=var(--color-prettylights-syntax-constant)]= { [color=var(--color-prettylights-syntax-string)]"key_id":[color=var(--color-prettylights-syntax-string)]"ec02", [color=var(--color-prettylights-syntax-string)]"hexsign":o, [color=var(--color-prettylights-syntax-string)]"context[ts]":[color=var(--color-prettylights-syntax-string)]f"{int(time.time())}", [color=var(--color-prettylights-syntax-string)]"csrf": [color=var(--color-prettylights-syntax-string)]'' } headers [color=var(--color-prettylights-syntax-constant)]= { [color=var(--color-prettylights-syntax-string)]'user-agent': [color=var(--color-prettylights-syntax-string)]"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0" } resp [color=var(--color-prettylights-syntax-constant)]= requests.[color=var(--color-prettylights-syntax-entity)]post(url, params[color=var(--color-prettylights-syntax-constant)]=params,headers[color=var(--color-prettylights-syntax-constant)]=headers).[color=var(--color-prettylights-syntax-entity)]json() [color=var(--color-prettylights-syntax-comment)]#{'code': 0, 'message': 'OK', 'data': {'ticket': 'eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDI3NDI3NDYsImlhdCI6MTcwMjQ4MzQ4NiwicGx0IjotMX0.xQgtTAc41NA1gzvd9yKUPgucUy_DKcQj6OG1vj8V7ZA', 'created_at': 1702483486, 'ttl': 259200, 'context': {}, 'nav': {'img': 'https://i0.hdslb.com/bfs/wbi/7cd084941338484aae1ad9425b84077c.png', 'sub': 'https://i0.hdslb.com/bfs/wbi/4932caff0ff746eab6f01bf08b70ac45.png'}}, 'ttl': 1}
|
|