郵件驗證碼 (MAC)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
透過 MAC 原始碼,您可以驗證資料是否遭到竄改。寄件者與收件者共用對稱金鑰時,可以為特定郵件計算驗證標記,讓收件者驗證郵件是否來自預期的寄件者,且未經修改。
MAC 具有下列屬性:
- 真實性:知道金鑰是建立可驗證的 MAC 標記的唯一方法。
- Symmetric:計算及驗證代碼時,需要使用相同的金鑰。
視演算法而定,MAC 可為確定性或隨機性。Tink 目前不會導入非確定性 MAC 演算法。您應僅將 MAC 用於郵件驗證,而非用於產生偽隨機位元組等其他用途 (請參閱 PRF)。
如果您需要非對稱原始碼,請參閱「數位簽名」。
選擇金鑰類型
我們建議您在大多數情況下使用 HMAC_SHA256,但也有其他選項。
一般來說,下列情況成立:
最低安全性保證
- 至少 80 位元驗證強度
- 在選定明文攻擊下防範存在偽造
- 至少提供 128 位元安全性,可防範金鑰復原攻擊,並支援多使用者情境 (當攻擊者並非鎖定特定金鑰,而是鎖定最多 232 組金鑰中的任何金鑰時)
用途範例
請參閱「我想保護資料免於遭到竄改」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eThe Message Authentication Code (MAC) primitive, using a shared symmetric key, enables verification of data integrity and authenticity by generating an authentication tag.\u003c/p\u003e\n"],["\u003cp\u003eMAC ensures authenticity as only the key holder can create a verifiable tag, and it's symmetric, requiring the same key for both computation and verification.\u003c/p\u003e\n"],["\u003cp\u003eTink recommends HMAC_SHA256 for most use cases, while HMAC_SHA512 offers higher security and AES256_CMAC might provide better performance with specific hardware.\u003c/p\u003e\n"],["\u003cp\u003eTink's MAC implementation guarantees a minimum of 80-bit authentication strength, protection against forgery, and at least 128-bit security against key recovery attacks, even in multi-user environments.\u003c/p\u003e\n"]]],["MAC uses a shared symmetric key between sender and recipient to verify message authenticity and integrity. The sender computes an authentication tag for a message, which the recipient uses to confirm its origin and unaltered state. MAC guarantees authenticity, where only key holders can create verifiable tags. Key recommendations include HMAC_SHA256, HMAC_SHA512 (most conservative), and AES256_CMAC (fastest with AES-NI). MAC is designed solely for message authentication, offering a minimum of 80-bit authentication strength.\n"],null,["# Message Authentication Code (MAC)\n\nThe MAC primitive lets you to verify that no one has tampered with your data.\nA sender sharing a *symmetric key* with a recipient can compute an\n*authentication tag* for a given message, which allows the recipient to verify\nthat a message is from the expected sender and has not been modified.\n\nMAC has the following properties:\n\n- **Authenticity**: Knowing the key is the only way to create a verifiable MAC tag.\n- **Symmetric**: Computing and verifying the tag requires the same key.\n\nMAC can be deterministic or randomized, depending on the algorithm. Tink does\nnot implement non-deterministic MAC algorithms at the moment. You should use MAC\nonly for message authentication, not for other purposes like generation of\npseudorandom bytes (for that, see [PRF](/tink/prf)).\n\nIf you need an asymmetric primitive instead, see [Digital\nSignature](/tink/digital-signature).\n\n### Choose a key type\n\nWe recommend using **HMAC_SHA256** for most uses, but there are other options as\nwell.\n\nIn general, the following holds true:\n\n- HMAC_SHA512 may or may not be faster depending on your message size and the specifics of the hardware you use.\n- HMAC_SHA512 is the most conservative mode that can be used for practically unlimited number of messages.\n- AES256_CMAC is fastest on systems that support the [AES-NI](https://www.intel.com/content/www/us/en/developer/articles/technical/advanced-encryption-standard-instructions-aes-ni.html) hardware\n acceleration.\n\n | **Note:** Tink doesn't offer AES128_CMAC because it doesn't offer 128-bit security in multi-user scenarios.\n\n### Minimal security guarantees\n\n- At least 80-bit authentication strength\n- Secure against existential forgery under chosen plaintext attack\n- At least 128-bit security against key recovery attacks, and also in multi-user scenarios (when an attacker is not targeting a specific key, but any key from a set of up to 2^32^ keys)\n\n### Example use case\n\nSee [I want to protect data from tampering](/tink/protect-data-from-tampering)."]]