d157e0d7f137c9ffc8d65473e038ee86 是 “Hello world !” 使用 “mykey”作为key的签名结果
>>> import hmac >>> print hmac.new("mykey","Hello world !").hexdigest() d157e0d7f137c9ffc8d65473e038ee86 #下面是一个简单的C/S程序,使用了hmac签名 # 客户端 (signs the data) import xmlrpclib,hmac,hashlib key = "mysecret" server = xmlrpclib.ServerProxy("http://localhost:8888") name = "Homer" signature = hmac.new(key,name).hexdigest() print server.sayHello(signature,name) # 服务器 (verifies the signature) import SimpleXMLRPCServer,hmac,hashlib key = "mysecret" class MyClass: def sayHello(self, signature, name): if hmac.new(key,name).hexdigest() != signature: return "Wrong signature ! You're a hacker !" else: return u"Hello, %s !" % name server_object = MyClass() server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8888)) # (2) server.register_instance(server_object) # (3) print "Listening on port 8888" server.serve_forever()