接口加密思路

一、发送的思路

  • 利用import hashlib,import time,import requests这三个模块

根据时间是戳是唯一的做md5的加密,

  • 获取当前时间,当前时间和字符串拼接
import  requests
import hashlib
import time

#获取当前时间
#当前时间和字符串拼接
current_time = time.time()
app_id = "299095cc-1330-11e5-b06a-a45e60bec08b"
v = "%s|%s"%(app_id,current_time)
  • 对拼接的字符串加密
m = hashlib.md5()
m.update(bytes(v,encoding="utf-8"))
authkey = m.hexdigest()
  • 把当前时间和加密好的字符串进行拼接

authkey_time = "%s|%s"%(authkey,current_time)

  • 通过requests发送
host_data = {
    'status':True,
    'data':{
        'hostname':'c1.com',
        'disk':{'status':True,'data':'xxxxx'},
        'men':{'status':True,'data':'xxxxx'},
        'nic':{'status':True,'data':'xxxxx'},
    }
}


#通过请求头返回给后台
response = requests.post(
    url='http://127.0.0.1:8000/api/asset/',
    json=host_data,
    headers = {'authkey':authkey_time}
)

print(response.text)

二、接受的思路

  • post的请求通过request.body来获取,header通过request.META获取
def asset(request):
    if request.method == "POST":
        auth_list = []
        # 获取post请求过来的参数
        host_info = json.loads(str(request.body, encoding="utf-8"))
        #获取header发送过来的加密字符串
        auth_key_time = request.META['HTTP_AUTHKEY']
        #通过分割,分别获取md5的加密和脚本的时间
        auth_key, client_ctime = auth_key_time.split("|")
        server_time =time.time()
  • 第一层判断,判断服务器时间和脚本时间的时间差
  • 第二层判断,判断同样的时间戳确保只能访问一次
  • 服务器端的ck+脚本时间戳md5的加密但等同于脚本的md5
  • 把用过的时间戳假如,超过十秒的排除
 if server_time-10>float(client_ctime):
            return HttpResponse('访问失败')
        elif auth_key_time in auth_list:
            return HttpResponse('访问失败')


        v = "%s|%s" % (ck, client_ctime)
        m = hashlib.md5()
        m.update(bytes(v, encoding="utf-8"))
        server_key = m.hexdigest()

        if server_key !=host_info:
            return HttpResponse('无权限')

        auth_list.append(client_ctime)
        for i in auth_list:
            if server_time-10>float(client_ctime):
                auth_list.pop(i)

三、代码

脚本

import  requests
import hashlib
import time

#获取当前时间
#当前时间和字符串拼接
current_time = time.time()
app_id = "299095cc-1330-11e5-b06a-a45e60bec08b"
v = "%s|%s"%(app_id,current_time)

#对拼接的字符串加密
m = hashlib.md5()
m.update(bytes(v,encoding="utf-8"))
authkey = m.hexdigest()


#把当前时间和加密好的字符串进行拼接
authkey_time = "%s|%s"%(authkey,current_time)



host_data = {
    'status':True,
    'data':{
        'hostname':'c1.com',
        'disk':{'status':True,'data':'xxxxx'},
        'men':{'status':True,'data':'xxxxx'},
        'nic':{'status':True,'data':'xxxxx'},
    }
}


#通过请求头返回给后台
response = requests.post(
    url='http://127.0.0.1:8000/api/asset/',
    json=host_data,
    headers = {'authkey':authkey_time}
)

print(response.text)

服务器端

def asset(request):
    if request.method == "POST":
        auth_list = []
        host_info = json.loads(str(request.body, encoding="utf-8"))
        auth_key_time = request.META['HTTP_AUTHKEY']
        auth_key, client_ctime = auth_key_time.split("|")
        server_time =time.time()
        if server_time-10>float(client_ctime):
            return HttpResponse('访问失败')
        elif auth_key_time in auth_list:
            return HttpResponse('访问失败')


        v = "%s|%s" % (ck, client_ctime)
        m = hashlib.md5()
        m.update(bytes(v, encoding="utf-8"))
        server_key = m.hexdigest()

        if server_key !=host_info:
            return HttpResponse('无权限')

        auth_list.append(client_ctime)
        for i in auth_list:
            if server_time-10>float(client_ctime):
                auth_list.pop(i)



        return HttpResponse('.....')
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,309评论 19 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 12,095评论 6 13
  • 一、GET/POST方法简介&用户安全 1、GET/POST方法简介 在客户端和服务器之间进行请求-响应时,两种最...
    方圆十里不留母狗阅读 32,531评论 0 13
  • 一千块钱是你的,一万块钱是你的,一百万也是你的,但是十个亿就不是你的。这些钱是老百姓相信你才让你挣的,你替他们支配...
    大杨莎子阅读 1,010评论 0 0
  • (闲游)风和日丽山河美,别样风景更迷人。蓝天白云这里有,青草娇花湖水淸。心自放飞处处景,白衣黑裤悠悠然。岁月轻步慢...
    甘朝武阅读 191评论 0 0

友情链接更多精彩内容