รหัสการตรวจสอบสิทธิ์ข้อความ (MAC)
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
องค์ประกอบพื้นฐาน MAC ช่วยให้คุณยืนยันได้ว่าไม่มีใครได้แทรกแซงข้อมูลของคุณ
ผู้ส่งที่แชร์คีย์แบบสมมาตรกับผู้รับสามารถคํานวณแท็กการตรวจสอบสิทธิ์สําหรับข้อความหนึ่งๆ ซึ่งจะช่วยให้ผู้รับยืนยันได้ว่าข้อความนั้นมาจากผู้ส่งที่คาดไว้และไม่มีการแก้ไข
MAC มีพร็อพเพอร์ตี้ต่อไปนี้
- ความถูกต้อง: การทราบคีย์เป็นวิธีเดียวในการสร้างแท็ก MAC ที่ตรวจสอบได้
- Symmetric: การประมวลผลและการยืนยันแท็กต้องใช้คีย์เดียวกัน
MAC อาจเป็นแบบกำหนดได้หรือแบบสุ่ม ทั้งนี้ขึ้นอยู่กับอัลกอริทึม ปัจจุบัน Tink ไม่ได้ใช้อัลกอริทึม MAC ที่ไม่แน่นอน คุณควรใช้ MAC เฉพาะสําหรับการตรวจสอบสิทธิ์ข้อความเท่านั้น ไม่ใช่เพื่อวัตถุประสงค์อื่นๆ เช่น การสร้างไบต์แบบสุ่มจำลอง (โปรดดูPRF)
หากต้องการใช้รูปแบบที่ไม่สมมาตรแทน โปรดดูลายเซ็นดิจิทัล
เลือกประเภทคีย์
เราขอแนะนำให้ใช้ HMAC_SHA256 สําหรับการใช้งานส่วนใหญ่ แต่ก็มีตัวเลือกอื่นๆ ด้วย
โดยทั่วไปแล้ว ข้อมูลต่อไปนี้เป็นจริง
การรับประกันความปลอดภัยขั้นต่ำ
- ระดับการตรวจสอบสิทธิ์อย่างน้อย 80 บิต
- ปลอดภัยจากการปลอมแปลงที่เป็นไปได้ภายใต้การโจมตีข้อความธรรมดาที่เลือก
- การรักษาความปลอดภัยอย่างน้อย 128 บิตเพื่อป้องกันการโจมตีเพื่อกู้คืนคีย์ และในกรณีที่มีผู้ใช้หลายคน (เมื่อผู้โจมตีไม่ได้มุ่งเป้าไปที่คีย์ใดคีย์หนึ่งโดยเฉพาะ แต่มุ่งเป้าไปที่คีย์ใดก็ได้จากชุดคีย์ที่มีไม่เกิน 232 คีย์)
ตัวอย่างกรณีการใช้งาน
โปรดดูหัวข้อฉันต้องการปกป้องข้อมูลจากการดัดแปลง
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 2025-07-25 UTC"],[[["\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)."]]