自学内容网 自学内容网

前后端交互常用的时间敏感算法

前后端交互常用的时间敏感算法

前后端交互中涉及时间敏感的算法主要用于确保数据传输的安全性、有效性和同步性。以下是一些常见的时间敏感算法和技术:

1. 基于时间戳的签名算法(HMAC):

描述: HMAC(哈希消息认证码)是一种基于散列函数的消息认证码,通过在消息中添加时间戳,可以防止重放攻击。
应用: 用于API请求的认证和授权。例如,在请求头中包含时间戳和HMAC签名,服务器可以验证请求的时间戳是否在允许的时间窗口内。

import hmac
import hashlib
import time
import base64


def generate_signature(secret_key, message, timestamp):
    # 创建带有时间戳的消息
    message_with_timestamp = f"{message}:{timestamp}"

    # 使用HMAC和SHA256哈希算法生成签名
    signature = hmac.new(secret_key.encode(), message_with_timestamp.encode(), hashlib.sha256).digest()

    # 将签名编码为Base64格式
    signature_base64 = base64.b64encode(signature).decode()

    return signature_base64


def verify_signature(secret_key, message, timestamp, signature, time_window=60):
    # 获取当前时间戳
    current_timestamp = int(time.time())

    # 检查时间戳是否在允许的时间窗口内
    if abs(current_timestamp - int(timestamp)) > time_window:
        return False

    # 生成待验证的签名
    expected_signature = generate_signature(secret_key, message, timestamp)

    # 比较生成的签名和传入的签名是否匹配
    return hmac.compare_digest(expected_signature, signature)


# 使用示例
secret_key = "your_secret_key"
message = "your_message"
timestamp = str(int(time.time()))

# 生成签名
signature = generate_signature(secret_key, message, timestamp)
print(f"Generated Signature: {signature}")

# 验证签名
is_valid = verify_signature(secret_key, message, timestamp, signature)
print(f"Is signature valid? {is_valid}")

# 模拟延迟,测试签名失效
time.sleep(61)
is_valid_expired = verify_signature(secret_key, message, timestamp, signature)
print(f"Is signature valid after delay? {is_valid_expired}")

2. 时间戳防篡改技术:

描述: 在请求或数据中附加时间戳,并使用加密技术确保时间戳的完整性。
应用: 防止数据篡改和重放攻击。例如,支付系统中的交易请求通常会包含一个签名的时间戳。

3. 令牌(Token)过期机制:

描述: 通过设置令牌的有效期来控制其使用时间,过期后需要重新获取新的令牌。
应用: JWT(JSON Web Token)和OAuth等认证授权机制。JWT通常包含一个有效期字段(exp),服务器会验证令牌是否过期。

4. 基于时间的OTP(一次性密码):

描述: TOTP(基于时间的一次性密码)是一种两步验证机制,通过当前时间生成一次性密码,通常每30秒更新一次。
应用: 用于双因素认证(2FA),提高账户安全性。

import time
import hmac
import hashlib
import struct
import base64


def get_totp_token(secret, time_step=30, digits=6):
    # 将密钥解码为字节
    key = base64.b32decode(secret, True)

    # 获取当前时间的步数
    current_time = int(time.time() // time_step)

    # 将时间步数打包为字节格式
    msg = struct.pack(">Q", current_time)

    # 使用HMAC-SHA1算法生成哈希值
    h = hmac.new(key, msg, hashlib.sha1).digest()

    # 提取哈希值的一部分
    o = h[19] & 15
    h = (struct.unpack(">I", h[o:o + 4])[0] & 0x7fffffff)
    print(h)
    # 获取动态密码的数字部分
    token = h % (10 ** digits)

    return str(token).zfill(digits)


# 示例使用
secret = "JBSWY3DPEHPK3PXP"  # 这是一个示例密钥,应为双方共享
token = get_totp_token(secret)
print("TOTP Token:", token)
print(token == '')

5. 时间同步协议(NTP):

描述: NTP(网络时间协议)用于同步计算机系统之间的时钟,以确保前后端在同一个时间基准下运行。
应用: 分布式系统和微服务架构中,确保各个服务之间的时间一致性,防止由于时间不同步引起的错误。

6. 基于时间的缓存策略:

描述: 设置缓存数据的有效期(TTL),确保缓存的数据在特定时间内是有效的。
应用: 前端页面缓存和CDN缓存等,通过设定缓存时间来优化性能并减少服务器负载。

这些时间敏感的算法和技术在实际应用中通常会结合使用,以提供更高的安全性和可靠性。例如,在一个RESTful API的交互过程中,可能会同时使用HMAC签名、时间戳防篡改和令牌过期机制来确保请求的安全性和有效性。


原文地址:https://blog.csdn.net/weixin_44777680/article/details/140145206

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!