HTTP Basic和Digest认证介绍与计算
一、说明
web用户认证,最开始是get提交+把用户名密码存放在客户端的cookie中的形式;在意识到这样不安全之后逐渐演变成了post提交+把用户凭证放到了服务端的session中的形式(当然sessionid还在cookie中)。
不过其实最初给http设计的认证方式,既不是“get+cookie”也不是“post+session”,而是Basic和Digest。但Basic和Digest并不流行我想主要是因为麻烦,一是说Basic和Digest使用的Authorization头并不会被浏览器自动发往服务器,二是说对于Digest计算很麻烦。
二、Basic认证形式
2.1 Basic认证请求示例
请求示例如下,主要是Authorization头(位置不重要,http头一般都不分先后)
GET /GetDeviceInfo HTTP/1.1
Host: 192.168.220.128
Authorization: Basic YWRtaW46MTIzNDU2
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept-Encoding: gzip, deflate
Accept: */*
Cache-Control: no-cache
Cookie: Secure
Connection: close
2.2 Basic认证计算方法
前边请求Authorization头的YWRtaW46MTIzNDU2,实际上是用户名admin密码123456使用以下计算方法得到:
base64(username:password)
Python计算代码如下:
import base64 def get_basic_authorization_header_value(username,password):
# base64编码前后都(要)是字节码形式
authorization_value = base64.b64encode((f"{username}:{password}").encode()).decode()
authorization_header_value = f"Basic {authorization_value}"
return authorization_header_value
三、Digest认证形式
3.1 Digest认证请求示例
GET /GetDeviceInfo HTTP/1.1
Host: 192.168.220.128
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0
Authorization: Digest username="admin",realm="TVT API Test Tool",nonce="d4f95e85dc5a39a4914db61b67878f5b",uri="GetDeviceInfo",algorithm="MD5",cnonce="d4f95e85dc5a39a4914db61b67878f5b",nc=00000001,qop="auth",response="1cc4cf126d3c4a70d2de34c5d8c2943c"
Accept-Encoding: gzip, deflate
Accept: */*
Cache-Control: no-cache
Cookie: Secure
Connection: close
username----系统用户名;客户端自行填充
realm----领域;服务端通过WWW-Authenticate头返回内容可以自己随便定,但其目的是用于提示客户端当前是什么系统,所以规范来说应类似于“myhost@testrealm.com”的形式。
nonce----服务端通过WWW-Authenticate头返回的随机数
uri----请求接口或资源(似乎规范来说应用GET或POST后的一样,上边例子中少了/是因为服务端没按规范实现)
algorithm----后边response用的计算方法
cnonce----client nonce,客户端生成的随机数
nc----nonce count,用于标识进行请求的次数。(但你一直不变服务端也不会管你对不对)
qop----quality of protection,进一步限定response的计算方法,服务端通过WWW-Authenticate头返回。
response----认证最主要的值,前面各字段除algorithm外全要参与该值的计算。
3.2 Digest认证计算方法
在最开始的RFC 2069中规定response计算方法如下:
HA1 = MD5(username:realm:password)
HA2 = MD5(method:uri)
response = MD5(HA1:nonce:HA2)
随后的RFC 2617对计算方法进行了增强,规定计算方法如下(当algorithm值为MD5或未指定、qop未指定时等同RFC 2069):
# HA1部分
# 当algorithm值为"MD5"或未指定时,HA1计算方法如下
HA1 = MD5(username:realm:password)
# 当algorithm值为"MD5-sess"时,HA1计算方法如下
HA1 = MD5(MD5(username:realm:password):nonce:cnonce) # HA2部分
# 当qop值为"auth"或未指定时,HA2计算方法如下
HA2 = MD5(method:uri)
# 当qop值为"auth-int"时,HA2计算方法如下;entityBody是指整个body(?)
HA2 = MD5(method:uri:MD5(entityBody)) # response部分
# 当qop值为"auth"或"auth-int"时,response计算方法如下
response = MD5(HA1:nonce:nonceCount:cnonce:qop:HA2)
# 当qop未指定时,response计算方法如下
response = MD5(HA1:nonce:HA2)
Python计算代码如下:
import hashlib # body初始值不要是None,不然下边encode时会报错
def get_basic_authorization_header_value(username, password, uri, method, realm, nonce, nc, cnonce, algorithm=None, qop=None, body=""):
response_value = calc_digest_response_value(username, password, uri, method, realm, nonce, nc, cnonce, algorithm, qop, body)
authorization_header_value = f'Digest username="{username}",realm="{realm}",nonce="{nonce}",uri="{uri}",algorithm="{algorithm}",cnonce="{cnonce}",nc={nc},qop="{qop}",response="{response_value}"',
return authorization_header_value def calc_digest_response_value(username, password, uri, method, realm, nonce, nc, cnonce, algorithm=None, qop=None, body=""):
# HA1部分
# 当algorithm值为"MD5"或未指定时,HA1计算方法如下
if algorithm == "MD5" or algorithm == "" or algorithm is None:
HA1 = hashlib.md5((f"{username}:{realm}:{password}").encode()).hexdigest()
# 当algorithm值为"MD5-sess"时,HA1计算方法如下
elif algorithm == "MD5-sess":
HA1 = hashlib.md5((f"{username}:{realm}:{password}").encode()).hexdigest()
HA1 = hashlib.md5((f"{HA1}:{nonce}:{cnonce}").encode()).hexdigest()
else:
response_value = '"the value of algorithm must be one of "MD5"/"MD5-sess"/""/None'
return response_value # HA2部分
# 当qop值为"auth"或未指定时,HA2计算方法如下
if qop == "auth" or qop == "" or qop is None:
HA2 = hashlib.md5((f"{method}:{uri}").encode()).hexdigest()
# 当qop值为"auth-int"时,HA2计算方法如下;entityBody是不是指整个body我其实不太确定
elif qop == "auth-int":
HA2 = hashlib.md5((f"{body}").encode()).hexdigest()
HA2 = hashlib.md5((f"{method}:{uri}:{HA2}").encode()).hexdigest()
else:
response_value = '"the value of qop must be one of "auth"/"auth-int"/""/None'
return response_value # response部分
# 当qop值为"auth"或"auth-int"时,response计算方法如下
if qop == "auth" or qop == "auth-int":
response_value = hashlib.md5((f"{HA1}:{nonce}:{nc}:{cnonce}:{qop}:{HA2}").encode()).hexdigest()
# 当qop未指定时,response计算方法如下
elif qop == "" or qop is None:
response_value = hashlib.md5((f"{HA1}:{nonce}:{HA2}").encode()).hexdigest()
else:
response_value = "unknown error"
return response_value
参考:
https://en.wikipedia.org/wiki/Digest_access_authentication
https://tools.ietf.org/html/rfc2069
https://tools.ietf.org/html/rfc2617
HTTP Basic和Digest认证介绍与计算的更多相关文章
- HTTP认证模式:Basic and Digest Access Authentication
一. Basic 认证 客户端以“ : ”连接用户名和密码后,再经BASE64编码( Base64 Content-Transfer-Encoding )通过Authorization请求头发送该密文 ...
- http 登录Digest认证相关知识
Digest access authentication https://en.wikipedia.org/wiki/Digest_access_authentication Digest acces ...
- 【ASP.NET Web API2】Digest认证
Digest摘要认证 对于Basic认证方案来说,被传输的认证凭证仅仅采用Base64编码,所以包含密码的认证凭证基本上可以视为明文传输.Digest认证在此基础上进行了改善,在一定程度上提高了安全系 ...
- OAuth2.0认证介绍
OAuth2.0鉴权 返回 目录 [隐藏] 1 腾讯微博OAuth2.0认证介绍 2 获取accesstoken的两种方式 2.1 1.Authorization code grant 2.1.1 第 ...
- 批量检测GoAhead系列服务器中Digest认证方式的服务器弱口令
最近在学习用python写爬虫工具,某天偶然发现GoAhead系列服务器的登录方式跟大多数网站不一样,不是采用POST等方法,通过查找资料发现GoAhead是一个开源(商业许可).简单.轻巧.功能强大 ...
- 使用crypt配置Basic Auth登录认证
简介 Basic Auth用于服务端简单的登录认证,通常使用服务器Nginx.Apache本身即可完成.比如我们要限定某个域名或者页面必须输入用户名.密码才能登录,但又不想使用后端开发语言,此时Bas ...
- 本文将介绍“数据计算”环节中常用的三种分布式计算组件——Hadoop、Storm以及Spark。
本文将介绍“数据计算”环节中常用的三种分布式计算组件——Hadoop.Storm以及Spark. 当前的高性能PC机.中型机等机器在处理海量数据时,其计算能力.内存容量等指标都远远无法达到要求.在大数 ...
- 任务31:课时介绍 & 任务32:Cookie-based认证介绍 &任务33:34课 :AccountController复制过来没有移除[Authorize]标签
任务31:课时介绍 cookie-based网站这边的认证 jwt基于移动端和前后端分离的项目,jwt有一些独特的优势 jwt在asp.net core中的实现机制,了解实现机制并进行扩展.比如非标准 ...
- 接口开发---basic auth接口认证
开发中遇到了basic auth来认证的案例,这里总结一下: Basic Auth简单点说明就是每次请求API时都提供用户的username和password.[base64encode(userna ...
随机推荐
- centos安装图形界面
1.首先安装X(X Window System),命令为 yum groupinstall "X Window System" 回车 2.由于这个软件组比较大,安装过程会比较 ...
- hashlib hmac configparser subprocess xlrd xlwt
hashlib模块:加密 import hashlib # 基本使用 cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8')) print(ciphe ...
- MyTests
目录 About Tests Selenium自动化测试 Pyppeteer Explain About Tests 扯淡!测试之瞎扯淡 Selenium自动化测试 什么是Selenium? Sele ...
- linux 安装oracle
报错1:报display相关错误su - oracleDISPLAY=redhat64-2:1 --此处为远程会话地址(可以写localhost或IP均可)export DISPLAYcd /home ...
- [Python数据挖掘]第4章、数据预处理
数据预处理主要包括数据清洗.数据集成.数据变换和数据规约,处理过程如图所示. 一.数据清洗 1.缺失值处理:删除.插补.不处理 ## 拉格朗日插值代码(使用缺失值前后各5个未缺失的数据建模) impo ...
- 【转载】opencv实现人脸检测
全文转载自CSDN的博客(不知道怎么将CSDN的博客转到博客园,应该没这功能吧,所以直接复制全文了),转载地址如下 http://blog.csdn.net/lsq2902101015/article ...
- 场景:如果一个select下拉框的值被选中,其他两个字段值的校验也生效
$("#operationType").change(function(){ if(this.value==1){ $('[name="assigneeCardType& ...
- Nilearn 小记
4.绘制脑图像 4.1 绘图功能 当打开了太多图像而不关闭时,会出现如下问题: 每次调用绘图函数都会创建一个新图像.当在非交互式设置(例如脚本或程序)中使用时,这些图像不会显示,但会常驻于内存中并最终 ...
- 剑指offer:链表中倒数第k个结点
问题描述 输入一个链表,输出该链表中倒数第k个结点. 解题思路 两个指针都指向头结点,第一个指针先移动k-1个结点,之后两指针同时移动,当第一个指针到链表尾的时候,第二个指针刚好指向倒数第k个结点. ...
- HTML5外包注意事项-开发HTML5游戏的九大坑与解决方法剖析
随着移动社区兴起,势必带动HTML5的革命.未来一两年内,HTML5移动游戏必将呈现大爆发趋势. 以下是整理的HTML5游戏研发.市场趋势以及渠道布局和技术解决方案的内容.希望大家能从本文中找到对HT ...