Content:
+ De-identification(去識別化)
+ Hash function
+ Message authentication code (訊息鑑別碼)
+ Digital signature (數位簽章)
+ Digital certificate (數位憑證)
+ HTTP Authentication
+ Two Factor Authentication (2FA, 雙重認證)
De-identification(去識別化)
- Pseudonymisation(擬匿名化) => 真名換成暫時ID
- K-anonymization(K-匿名化) => 在查詢下,能鎖定的目標至少有有K個個體
Hash function
- Hash function 是把輸入的數據轉換成「固定長度」且「不規則」的函數
- 應用:密碼以雜湊碼保存在伺服器、產生
MAC
MAC (message authentication code): 包括
HMAC
(常用),OMAC
,CMAC
特徵
- 長度不變
- 相同的輸入必得相同的輸出
- 類似的輸入不會產生類似的輸出
- 可能發生 (低機率) 雜湊碰撞 (hash collision)
- 輸出無法反推導輸入
代表演算法
MD5 (deprecated), SHA-2
Message authentication code (訊息鑑別碼)
- 通稱
MAC
- 目的:身份鑑別和檢查訊息完整性
- 應用:E.g. 向電商購買商品,傳送商品編號
- 包括
HMAC
(常用),OMAC
,CMAC
使用步驟
- 客戶先以安全的方式產生兩組密鑰(假設為A和B)給電商,「密鑰A」用於加密訊息 (E.g. 商品編號),「密鑰B」用於產生訊息識別碼。
- 客戶將被「密鑰A」加密後的密文再和「密鑰B」 hash 產生
MAC
- 將密文(含例如:商品編號)和
MAC
一同交給電商 - 電商將收到的密文和之前收到的「密鑰B」也 hash 產生出
MAC
MAC
相同即代表內容未被串改
問題
因為雙方都能產生 MAC
因此會有抵賴問題 => 用數位簽章解決
HMAC (Hash-based Message Authentication Code)
E.g JWT
Digital signature (數位簽章)
- 目的:產生不可抵賴性
- 原理:只有私鑰擁有者能產生數位簽章
流程
也使用公鑰和私鑰,但是使用方式和 public key crypto-system 稍微不同
- 由訊息傳送者A準備:公鑰P、私鑰S、訊息M (這裡和 public key crypto-system 不同)
- A 將「公鑰P」交給 B
- A 將「訊息M」用「私鑰S」加密=數位簽章
- A 將「訊息M」和「數位簽章」交給 B
- B用「公鑰P」解密,比較與收到的訊息是否一致
代表演算法
RSA
RSA 的公鑰、私鑰能對調使用
Digital certificate (數位憑證)
<空>
HTTP Authentication
Traditional approach: cookie-based server-side sessions
Steps:
- User visit a website
- User submits account/password to a server (first time)
- Server creates a session in the DB
- Server responses
session_id
to client by cookie session_id
will be saved in the browser’s cookie jar- User secondly visit the website (這時 request 會夾帶 cookie 傳至 server)
- Server 比對 session
Token-based auth
Steps:
- User visit a website
- User submits account/password to a server (first time)
- Server creates a JWT and Server saves only the private key
- Server responses JWT to client and JWT is stored in local storage
- User secondly visit the website (這時 JWT 會被加到 authorization header <prefix Bearer>)
什麼是 JWT?
此篇:https://chengr4.medium.com/%E4%BB%80%E9%BA%BC%E6%98%AFjwt-e0130cc7ad4f
References
- 開發者必備知識 — HTTP認證(HTTP Authentication)
- Fireship; Session vs Token Authentication in 100 Seconds (2020.10)
Two Factor Authentication (2FA, 雙重認證)
- An extra layer of security used when logging into websites or apps.
- Log in =
username
andpassword
+ provide another form of authentication that only you know or have access to.
Another form of authentication => E.g. TOTP, HOTP, 短信, 電話
TOTP (Time-based One Time Password)
- E.g. 使用 Google Authenticator

One Time Password = TOTP(K, T) = Truncate(HMAC-SHA-1(K, T))
HMAC-SHA-1: 一種 hash 函數
K (pre-shared key): Client and Server 事先定好的
secret key
T (Time): 現在的時間段,Google Authenticator 每30秒更新一次時間
Truncate: 把太長的值減短
驗證過程
- Server 傳送
pre-shared key
至 Client (總之雙方要先有一組相同的pre-shared key
,此過程只有第一次。) - Client 對
pre-shared key
和「時間段」使用HMAC
產生One Time Password
(in Google Authenticator),並將之傳給 Server,例如傳送56743
,同時 Server 也以相同的方式產生One Time Password (56743)
, - 與 Client 的
One Time Password (56743)
在 Server 內進行比較,相同即通過驗證 (response 通過訊息給 client)。 - 之後每次驗證重複 2, 3 step
Reference
[1] Citrix (2019). Tech Insight — TOTP
[2] Tao (2011)。Google账户两步验证的工作原理
[3] Traversy Media (2020). Two Factor Authentication | Node.js & Speakeasy
Other reference
[1] Jeremy Kithome (2020). Implementing two-factor authentication using Speakeasy