HTTP Digest 認證:原理剖析與服務端實現詳解
HTTP 協議中的 Digest 認證(摘要認證)是一種比 Basic 認證更安全的身份驗證機制,其核心設計是避免密碼明文傳輸,并通過動態隨機數(Nonce)防范重放攻擊。本文將結合標準協議流程與具體服務端代碼實現,深入解析 Digest 認證的技術細節。
一、Digest 認證的核心原理與流程
1.1 為什么需要 Digest 認證?
Basic 認證直接通過 Base64 編碼傳輸用戶名和密碼(如 username:password
),雖然編碼可逆但無加密,安全性極差。Digest 認證通過哈希算法(如 MD5)對密碼進行處理,客戶端僅傳輸密碼的哈希值(而非明文),且每次請求使用動態隨機數(Nonce),大幅提升了安全性。
1.2 標準協議流程(RFC 7616)
Digest 認證的核心是 “挑戰 - 響應” 模式,完整流程可分為 4 步:
步驟 1:客戶端首次請求受保護資源
客戶端訪問服務端的受保護路徑(如示例中的 /sd
),未攜帶認證信息。
步驟 2:服務端返回 401 挑戰(Challenge)
服務端返回狀態碼 401 Unauthorized
,并在 WWW-Authenticate
頭部中攜帶以下關鍵參數:
realm
:認證域(如MyProtectedSD
),用于提示用戶認證的上下文(如 “我的受保護存儲”)。nonce
:服務端生成的一次性隨機數(如a1b2c3...
),用于防止重放攻擊。opaque
:服務端生成的固定字符串(如 MD5 哈希值),客戶端需原樣返回。qop
:認證質量(Quality of Protection),常見值為auth
(僅驗證請求)。algorithm
:哈希算法(默認MD5
)。
示例頭部:
WWW-Authenticate: Digest realm="MyProtectedSD", qop="auth", nonce="a1b2c3...", opaque="d4e5f6...", algorithm="MD5"
步驟 3:客戶端構造認證響應(Response)
客戶端收到挑戰后,提示用戶輸入用戶名和密碼,計算并組裝 Authorization
頭部,包含以下參數:
-
username
:用戶輸入的用戶名。 -
realm
:服務端返回的realm
(需與本地存儲的密碼關聯)。 -
nonce
:服務端返回的nonce
(原樣使用)。 -
uri
:請求的資源路徑(如/sd
)。 -
response:核心哈希值,通過以下公式計算:
response = MD5(HA1:nonce:nc:cnonce:qop:HA2)
其中:
HA1 = MD5(username:realm:password)
(客戶端預計算的用戶密碼哈希)。HA2 = MD5(method:uri)
(請求方法與資源路徑的哈希,如GET:/sd
)。nc
:請求計數(Nonce Count,防止重放攻擊的遞增序號)。cnonce
:客戶端生成的隨機數(Client Nonce,增強隨機性)。
步驟 4:服務端驗證響應
服務端收到 Authorization
頭部后,根據以下邏輯驗證:
- 校驗
realm
、nonce
(是否有效且未過期)、opaque
是否匹配。 - 根據用戶名獲取存儲的密碼(或預計算的
HA1
)。 - 重新計算
HA1
、HA2
和期望的response
。 - 比較客戶端提供的
response
與服務端計算的response
,一致則認證成功。
二、服務端實現邏輯:代碼解析
本文提供的服務端代碼基于 Python 的 http.server
模塊,實現了 Digest 認證的核心邏輯。以下是關鍵模塊的詳細解析。
2.1 全局配置與狀態管理
# --- 配置 ---
SERVER_ADDRESS = '0.0.0.0' # 監聽所有接口,方便測試
SERVER_PORT = 8001
REALM = "MyProtectedSD" # 保護域名,會顯示在客戶端的認證提示中# 存儲用戶名和密碼(在實際應用中,密碼應該哈希存儲,這里為了演示方便直接存儲)
# 或者更好的方式是存儲 HA1 = MD5(username:realm:password)
# 例如: HA1 = hashlib.md5(f"your_username_here:{REALM}:your_password_here".encode()).hexdigest()
USERS_PASSWORDS = {"admin001": "my_password_random_generation"
}# 存儲已發出的 nonce 及其創建時間,用于校驗和防止重放
# {nonce_value: creation_timestamp}
# 實際應用中,nonce 應該有過期機制,并且用后即焚或嚴格校驗 nc (nonce count)
active_nonces = {}
NONCE_LIFETIME_SECONDS = 300 # Nonce 有效期,例如 5 分鐘# Opaque 值,服務器生成,客戶端原樣返回
OPAQUE = hashlib.md5(os.urandom(16)).hexdigest()
- 用戶存儲:示例中直接存儲明文密碼(實際生產環境應存儲預計算的
HA1 = MD5(username:realm:password)
)。 - nonce 管理:
active_nonces
字典記錄nonce
及其生成時間,用于后續過期校驗。 - opaque:服務端生成的固定字符串,防止客戶端篡改挑戰參數。
2.2 nonce 生成與過期校驗
def generate_nonce():"""生成唯一的 nonce 并記錄創建時間"""nonce = hashlib.md5((os.urandom(16).hex() + str(time.time())).encode()).hexdigest()active_nonces[nonce] = time.time() # 存儲 nonce 與時間戳return nonce
nonce
是 Digest 認證的安全基石,其生成需滿足:
- 隨機性:通過
os.urandom(16)
生成隨機字節,結合時間戳確保唯一性。 - 時效性:
active_nonces
記錄生成時間,超過NONCE_LIFETIME_SECONDS
(5 分鐘)后失效,防止重放攻擊。
2.3 挑戰響應(401 狀態碼)
def send_401_challenge(self, stale=False):"""發送 401 Unauthorized 響應和 WWW-Authenticate 挑戰頭"""self.send_response(401)self.send_header('Content-type', 'text/plain')nonce = generate_nonce() # 生成新 nonceauth_challenge = f'Digest realm="{REALM}", qop="auth", nonce="{nonce}", opaque="{OPAQUE}", algorithm="MD5"'if stale:auth_challenge += ', stale="true"' # 標記舊 nonce 過期,提示客戶端使用新 nonceself.send_header('WWW-Authenticate', auth_challenge)self.end_headers()self.wfile.write(b"Authentication required.")print(f"Sent 401 challenge with new nonce: {nonce}")
當客戶端未攜帶認證信息或認證失敗時,服務端返回 401
狀態碼,并通過 WWW-Authenticate
頭部發送挑戰參數。若 stale=True
(如 nonce
過期),客戶端會自動使用新 nonce
重試。
2.4 認證頭解析與驗證
2.4.1 解析 Authorization 頭部
def parse_digest_auth_header(auth_header_value):"""解析 Authorization: Digest ... 頭部字符串返回一個包含各參數的字典"""if not auth_header_value or not auth_header_value.lower().startswith('digest '):return Noneauth_parts = {}# 移除 "Digest " 前綴value_str = auth_header_value[len('Digest '):]# 使用正則表達式解析 key="value" 或 key=value 對# 這個正則表達式處理了帶引號和不帶引號的值pattern = re.compile(r'(\w+)=(?:"([^"]*)"|([^\s,]*))')for match in pattern.finditer(value_str):key = match.group(1)# 值可能在 group(2) (帶引號) 或 group(3) (不帶引號)val = match.group(2) if match.group(2) is not None else match.group(3)auth_parts[key] = valreturn auth_parts
客戶端發送的 Authorization
頭部是一個復雜的字符串(如 Digest username="admin001", realm="MyProtectedSD", nonce="a1b2c3", ...
),此函數通過正則表達式提取各參數,供后續驗證使用。
2.4.2 驗證認證響應
def verify_digest_response(self, auth_parts):"""校驗客戶端提供的 Digest 認證信息"""required_keys = ['username', 'realm', 'nonce', 'uri', 'response', 'qop', 'nc', 'cnonce']for key in required_keys:if key not in auth_parts:print(f"Missing digest auth key: {key}")return Falseusername = auth_parts.get('username')client_realm = auth_parts.get('realm')client_nonce = auth_parts.get('nonce')uri = auth_parts.get('uri')client_response = auth_parts.get('response')qop = auth_parts.get('qop')nc = auth_parts.get('nc') # nonce countcnonce = auth_parts.get('cnonce') # client noncealgorithm = auth_parts.get('algorithm', 'MD5').upper() # 默認為 MD5# 1. 校驗 Realmif client_realm != REALM:print(f"Realm mismatch: expected '{REALM}', got '{client_realm}'")return False# 2. 校驗 Nonce# 檢查 nonce 是否由服務器發出且未過期# 實際應用中,還需要檢查 nc (nonce count) 以防止重放攻擊 (nc 應該單調遞增)# 這里簡化處理:只檢查 nonce 是否存在且未超時if client_nonce not in active_nonces:print(f"Invalid nonce: {client_nonce} (not issued by server)")# 可以考慮發送 stale=true,讓客戶端用新 nonce 重試return "stale" # 特殊返回值表示 nonce 過期if time.time() - active_nonces[client_nonce] > NONCE_LIFETIME_SECONDS:print(f"Nonce expired: {client_nonce}")del active_nonces[client_nonce] # 刪除過期的 noncereturn "stale" # 特殊返回值表示 nonce 過期# 3. 校驗 Opaque (如果服務器在挑戰中發送了)if 'opaque' in auth_parts and auth_parts.get('opaque') != OPAQUE:print(f"Opaque mismatch")return False# 4. 獲取用戶密碼 (或預計算的 HA1)password = USERS_PASSWORDS.get(username)if not password:print(f"Unknown user: {username}")return False# 5. 計算 HA1# HA1 = MD5(username:realm:password)ha1_str = f"{username}:{REALM}:{password}"ha1 = hashlib.md5(ha1_str.encode('utf-8')).hexdigest()print(f"Calculated HA1 for {username}: {ha1}")# 6. 計算 HA2# HA2 = MD5(method:uri)# 注意: self.command 是 HTTP 方法 (e.g., "GET")# uri 是客戶端在 Authorization 頭中提供的 URIha2_str = f"{self.command}:{uri}"ha2 = hashlib.md5(ha2_str.encode('utf-8')).hexdigest()print(f"Calculated HA2 for {self.command}:{uri}: {ha2}")# 7. 計算期望的 response# response = MD5(HA1:nonce:nc:cnonce:qop:HA2)if qop == "auth" or qop == "auth-int": # auth-int 需要校驗 body,這里簡化expected_response_str = f"{ha1}:{client_nonce}:{nc}:{cnonce}:{qop}:{ha2}"else:# 如果 qop 不存在 (較老的 RFC 2069 規范,requests 不會這樣)expected_response_str = f"{ha1}:{client_nonce}:{ha2}"expected_response = hashlib.md5(expected_response_str.encode('utf-8')).hexdigest()print(f"Expected response: {expected_response}")print(f"Client response: {client_response}")# 8. 比較 responseif client_response == expected_response:# 認證成功后,可以考慮使當前 nonce 失效(或嚴格檢查 nc)# del active_nonces[client_nonce] # 如果 nonce 只能使用一次return Trueelse:print("Response mismatch.")return False
此函數是服務端認證的核心邏輯,通過 8 步校驗確保客戶端的合法性:
- 參數完整性:檢查必要參數(如
username
、nonce
)是否存在。 - realm 匹配:確保客戶端請求的認證域與服務端配置一致。
- nonce 有效性:驗證
nonce
是否由服務端生成且未過期(防止重放攻擊)。 - opaque 校驗:確保客戶端未篡改挑戰參數。
- 哈希計算:重新計算
HA1
(用戶密碼哈希)和HA2
(請求信息哈希),并生成期望的response
,與客戶端提供的response
比較。
2.5 請求處理(do_GET 方法)
def do_GET(self):parsed_path = urlparse(self.path)# 只對 /sd 路徑進行認證if parsed_path.path == '/sd':auth_header = self.headers.get('Authorization')if not auth_header:print("No Authorization header, sending 401 challenge.")self.send_401_challenge()returnauth_parts = parse_digest_auth_header(auth_header)if not auth_parts:print("Malformed Authorization header, sending 401 challenge.")self.send_401_challenge() # 或發送 400 Bad Requestreturnverification_result = self.verify_digest_response(auth_parts)if verification_result == "stale":print("Nonce was stale, sending 401 challenge with stale=true.")self.send_401_challenge(stale=True)elif verification_result:print("Authentication successful!")self.send_response(200)self.send_header('Content-type', 'application/json')self.end_headers()response_data = {"message": "Welcome to the secure data area!", "user": auth_parts.get('username')}import jsonself.wfile.write(json.dumps(response_data).encode('utf-8'))else:print("Authentication failed, sending 401 challenge again.")# 認證失敗,可以簡單地再次發送 401 (可能用新的 nonce)# 或者根據具體策略,如果嘗試次數過多可以發送 403 Forbiddenself.send_401_challenge()else:self.send_response(200)self.send_header('Content-type', 'text/plain')self.end_headers()self.wfile.write(b"This is an open area.")
服務端通過 do_GET
方法處理請求:
- 若請求路徑為
/sd
(受保護資源),則檢查Authorization
頭部。 - 無認證頭或解析失敗時,返回
401
挑戰。 - 認證成功后返回資源(如示例中的 JSON 數據)。
- 其他路徑(如根路徑)直接返回公開內容。
三、關鍵技術點與安全增強
3.1 nonce 的時效性與重放攻擊防范
- 時效性:
nonce
僅在NONCE_LIFETIME_SECONDS
(5 分鐘)內有效,過期后服務端刪除記錄,客戶端需重新獲取新nonce
。 - 重放攻擊:通過
nc
(請求計數)可以進一步防范 —— 客戶端每次使用同一nonce
時,nc
必須遞增(如從00000001
到00000002
),服務端若發現nc
未遞增或重復,則判定為重放攻擊。
3.2 密碼存儲的最佳實踐
示例中直接存儲明文密碼(僅為演示),實際生產環境應存儲預計算的 HA1
(MD5(username:realm:password)
)。這樣即使數據庫泄露,攻擊者也無法直接獲取密碼明文,需結合 realm
和 username
才能計算 HA1
,進一步增強安全性。
3.3 與 Basic 認證的對比
特性 | Basic 認證 | Digest 認證 |
---|---|---|
密碼傳輸方式 | Base64 編碼明文(可逆) | 哈希值(不可逆) |
防重放攻擊 | 不支持 | 支持(通過 nonce、nc) |
安全性 | 低(易被中間人截獲明文) | 高(無明文傳輸,動態隨機數) |
客戶端支持 | 所有主流瀏覽器 | 所有主流瀏覽器 |
四、測試與驗證
4.1 啟動服務端
運行代碼后,服務端監聽 0.0.0.0:8001
,保護路徑為 /sd
。
4.2 測試請求
使用 requests
模擬客戶端請求:
res = requests.get('http://127.0.0.1:8001/sd',auth=HTTPDigestAuth('admin001', 'my_password_random_generation'))
print(f"Status: {res.status_code}")
print(f"Request Authorization: {res.request.headers['Authorization']}")
print(f"Headers: {res.headers}")
print(f"Response Text: {res.text}")
requests
客戶端會自動處理挑戰 - 響應流程,請求后打印,可見如下響應信息:
Status: 200
Request Authorization: Digest username="admin001", realm="MyProtectedSD", nonce="f4b50139aeb242406e92e3a24a14f286", uri="/sd", response="72c8e7cf046193a3bce3fb80ec1ce4f6", opaque="b5a7e1bf338b6f1d6c70204c64fd9473", algorithm="MD5", qop="auth", nc=00000001, cnonce="0e22129e06725aa6"
Headers: {'Server': 'BaseHTTP/0.6 Python/3.12.9', 'Date': 'Thu, 22 May 2025 08:41:52 GMT', 'Content-type': 'application/json'}
Response Text: {"message": "Welcome to the secure data area!", "user": "admin001"}
服務端打印關鍵信息,可見客戶端請求流程及認證明細情況:
No Authorization header, sending 401 challenge.
127.0.0.1 - "GET /sd HTTP/1.1" 401 -
Sent 401 challenge with new nonce: f4b50139aeb242406e92e3a24a14f286
Calculated HA1 for admin001: fb5c1f99227711de645d65e5b091f978
Calculated HA2 for GET:/sd: 7c7e535b35fb1070562dec4be2da7ee5
Expected response: 72c8e7cf046193a3bce3fb80ec1ce4f6
Client response: 72c8e7cf046193a3bce3fb80ec1ce4f6
Authentication successful!
127.0.0.1 - "GET /sd HTTP/1.1" 200 -
若認證成功,服務端返回文本:
{"message": "Welcome to the secure data area!", "user": "admin001"}
4.3 驗證 nonce 過期
等待 5 分鐘后,若使用之前的 nonce
再次請求,服務端會返回 stale=true
的挑戰頭,提示客戶端使用新 nonce
。
五、服務端實現整體代碼
import hashlib
import time
import os
import re
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse# --- 配置 ---
SERVER_ADDRESS = '0.0.0.0' # 監聽所有接口,方便測試
SERVER_PORT = 8001
REALM = "MyProtectedSD" # 保護域名,會顯示在客戶端的認證提示中# 存儲用戶名和密碼(在實際應用中,密碼應該哈希存儲,這里為了演示方便直接存儲)
# 或者更好的方式是存儲 HA1 = MD5(username:realm:password)
# 例如: HA1 = hashlib.md5(f"your_username_here:{REALM}:your_password_here".encode()).hexdigest()
USERS_PASSWORDS = {"admin001": "my_password_random_generation"
}# 存儲已發出的 nonce 及其創建時間,用于校驗和防止重放
# {nonce_value: creation_timestamp}
# 實際應用中,nonce 應該有過期機制,并且用后即焚或嚴格校驗 nc (nonce count)
active_nonces = {}
NONCE_LIFETIME_SECONDS = 300 # Nonce 有效期,例如 5 分鐘# Opaque 值,服務器生成,客戶端原樣返回
OPAQUE = hashlib.md5(os.urandom(16)).hexdigest()def generate_nonce():"""生成一個唯一的 nonce 并記錄創建時間"""nonce = hashlib.md5((os.urandom(16).hex() + str(time.time())).encode()).hexdigest()active_nonces[nonce] = time.time() # 記錄 nonce 創建時間return noncedef parse_digest_auth_header(auth_header_value):"""解析 Authorization: Digest ... 頭部字符串返回一個包含各參數的字典"""if not auth_header_value or not auth_header_value.lower().startswith('digest '):return Noneauth_parts = {}# 移除 "Digest " 前綴value_str = auth_header_value[len('Digest '):]# 使用正則表達式解析 key="value" 或 key=value 對# 這個正則表達式處理了帶引號和不帶引號的值pattern = re.compile(r'(\w+)=(?:"([^"]*)"|([^\s,]*))')for match in pattern.finditer(value_str):key = match.group(1)# 值可能在 group(2) (帶引號) 或 group(3) (不帶引號)val = match.group(2) if match.group(2) is not None else match.group(3)auth_parts[key] = valreturn auth_partsclass DigestAuthHandler(BaseHTTPRequestHandler):def send_401_challenge(self, stale=False):"""發送 401 Unauthorized 響應和 WWW-Authenticate 挑戰頭"""self.send_response(401)self.send_header('Content-type', 'text/plain')nonce = generate_nonce() # 生成新 nonceauth_challenge = f'Digest realm="{REALM}", qop="auth", nonce="{nonce}", opaque="{OPAQUE}", algorithm="MD5"'if stale:auth_challenge += ', stale="true"' # 標記舊 nonce 過期,提示客戶端使用新 nonceself.send_header('WWW-Authenticate', auth_challenge)self.end_headers()self.wfile.write(b"Authentication required.")print(f"Sent 401 challenge with new nonce: {nonce}")def verify_digest_response(self, auth_parts):"""校驗客戶端提供的 Digest 認證信息"""required_keys = ['username', 'realm', 'nonce', 'uri', 'response', 'qop', 'nc', 'cnonce']for key in required_keys:if key not in auth_parts:print(f"Missing digest auth key: {key}")return Falseusername = auth_parts.get('username')client_realm = auth_parts.get('realm')client_nonce = auth_parts.get('nonce')uri = auth_parts.get('uri')client_response = auth_parts.get('response')qop = auth_parts.get('qop')nc = auth_parts.get('nc') # nonce countcnonce = auth_parts.get('cnonce') # client noncealgorithm = auth_parts.get('algorithm', 'MD5').upper() # 默認為 MD5# 1. 校驗 Realmif client_realm != REALM:print(f"Realm mismatch: expected '{REALM}', got '{client_realm}'")return False# 2. 校驗 Nonce# 檢查 nonce 是否由服務器發出且未過期# 實際應用中,還需要檢查 nc (nonce count) 以防止重放攻擊 (nc 應該單調遞增)# 這里簡化處理:只檢查 nonce 是否存在且未超時if client_nonce not in active_nonces:print(f"Invalid nonce: {client_nonce} (not issued by server)")# 可以考慮發送 stale=true,讓客戶端用新 nonce 重試return "stale" # 特殊返回值表示 nonce 過期if time.time() - active_nonces[client_nonce] > NONCE_LIFETIME_SECONDS:print(f"Nonce expired: {client_nonce}")del active_nonces[client_nonce] # 刪除過期的 noncereturn "stale" # 特殊返回值表示 nonce 過期# 3. 校驗 Opaque (如果服務器在挑戰中發送了)if 'opaque' in auth_parts and auth_parts.get('opaque') != OPAQUE:print(f"Opaque mismatch")return False# 4. 獲取用戶密碼 (或預計算的 HA1)password = USERS_PASSWORDS.get(username)if not password:print(f"Unknown user: {username}")return False# 5. 計算 HA1# HA1 = MD5(username:realm:password)ha1_str = f"{username}:{REALM}:{password}"ha1 = hashlib.md5(ha1_str.encode('utf-8')).hexdigest()print(f"Calculated HA1 for {username}: {ha1}")# 6. 計算 HA2# HA2 = MD5(method:uri)# 注意: self.command 是 HTTP 方法 (e.g., "GET")# uri 是客戶端在 Authorization 頭中提供的 URIha2_str = f"{self.command}:{uri}"ha2 = hashlib.md5(ha2_str.encode('utf-8')).hexdigest()print(f"Calculated HA2 for {self.command}:{uri}: {ha2}")# 7. 計算期望的 response# response = MD5(HA1:nonce:nc:cnonce:qop:HA2)if qop == "auth" or qop == "auth-int": # auth-int 需要校驗 body,這里簡化expected_response_str = f"{ha1}:{client_nonce}:{nc}:{cnonce}:{qop}:{ha2}"else:# 如果 qop 不存在 (較老的 RFC 2069 規范,requests 不會這樣)expected_response_str = f"{ha1}:{client_nonce}:{ha2}"expected_response = hashlib.md5(expected_response_str.encode('utf-8')).hexdigest()print(f"Expected response: {expected_response}")print(f"Client response: {client_response}")# 8. 比較 responseif client_response == expected_response:# 認證成功后,可以考慮使當前 nonce 失效(或嚴格檢查 nc)# del active_nonces[client_nonce] # 如果 nonce 只能使用一次return Trueelse:print("Response mismatch.")return Falsedef do_GET(self):parsed_path = urlparse(self.path)# 只對 /sd 路徑進行認證if parsed_path.path == '/sd':auth_header = self.headers.get('Authorization')if not auth_header:print("No Authorization header, sending 401 challenge.")self.send_401_challenge()returnauth_parts = parse_digest_auth_header(auth_header)if not auth_parts:print("Malformed Authorization header, sending 401 challenge.")self.send_401_challenge() # 或發送 400 Bad Requestreturnverification_result = self.verify_digest_response(auth_parts)if verification_result == "stale":print("Nonce was stale, sending 401 challenge with stale=true.")self.send_401_challenge(stale=True)elif verification_result:print("Authentication successful!")self.send_response(200)self.send_header('Content-type', 'application/json')self.end_headers()response_data = {"message": "Welcome to the secure data area!", "user": auth_parts.get('username')}import jsonself.wfile.write(json.dumps(response_data).encode('utf-8'))else:print("Authentication failed, sending 401 challenge again.")# 認證失敗,可以簡單地再次發送 401 (可能用新的 nonce)# 或者根據具體策略,如果嘗試次數過多可以發送 403 Forbiddenself.send_401_challenge()else:self.send_response(200)self.send_header('Content-type', 'text/plain')self.end_headers()self.wfile.write(b"This is an open area.")def log_message(self, format, *args):"""覆蓋默認日志,方便調試"""print(f"{self.address_string()} - {format % args}")def run_server(server_class=HTTPServer, handler_class=DigestAuthHandler, addr=SERVER_ADDRESS, port=SERVER_PORT):server_address = (addr, port)httpd = server_class(server_address, handler_class)print(f"Starting Digest Auth server on {addr}:{port}...")print(f"Protected path: /sd")print(f"Test with username: '{USERS_PASSWORDS.keys()}', password: '{USERS_PASSWORDS.values()}'")try:httpd.serve_forever()except KeyboardInterrupt:print("\nServer shutting down.")finally:httpd.server_close()if __name__ == '__main__':run_server()
小總結
HTTP Digest 認證通過哈希算法和動態隨機數(nonce)解決了 Basic 認證的明文傳輸問題,是輕量級場景下的安全認證方案。本文結合代碼詳細解析了其核心流程(挑戰 - 響應)和服務端實現邏輯(nonce 管理、哈希計算、響應驗證),并強調了生產環境中的安全增強點(如存儲 HA1
、校驗 nc
)。實際應用中,建議結合 HTTPS 進一步加密傳輸過程,以達到更高的安全性。
————————————————
Java猿社區—Http digest authentication 請求代碼最全示例 - 簡書
HTTP認證之摘要認證——Digest(二) - xiaoxiaotank - 博客園
HTTP的幾種認證方式之DIGEST 認證(摘要認證) - wenbin_ouyang - 博客園
HTTP Authentication之Basic認證、Digest認證
http digest鑒權流程
python http 身份認證簡介