1. 程式人生 > >hmac的檢驗客戶端合法性

hmac的檢驗客戶端合法性

key else rand accept color span pan iges egg

server端

import socket
import os
import hmac

seret_key = begg
sk = socket.socket()
sk.bind((127.0.0.1,8080))
sk.listen()

def check_conn(conn):
    msg = os.urandom(32)
    conn.send(msg)
    h = hmac.new(seret_key,msg)
    digest = h.digest()
    client_digest = conn.recv(1024)
    return hmac.compare_digest(digest,client_digest)
conn,addr 
= sk.accept() res = check_conn(conn) if res: print(合法的客戶端) conn.close() else: print(不合法的客戶端) conn.close() sk.close()

client端

import socket
import hmac

seret_key = begg
sk = socket.socket()
sk.connect((127.0.0.1,8080))
msg = sk.recv(1024)
h = hmac.new(seret_key,msg)
digest = h.digest()
sk.send(digest)

sk.close()

hmac的檢驗客戶端合法性