Mã xác thực thông báo (MAC)
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nguyên hàm MAC cho phép bạn xác minh rằng không ai can thiệp vào dữ liệu của bạn.
Người gửi chia sẻ khoá đối xứng với người nhận có thể tính toán thẻ xác thực cho một thư nhất định, cho phép người nhận xác minh rằng thư đến từ người gửi dự kiến và chưa bị sửa đổi.
MAC có các thuộc tính sau:
- Tính xác thực: Việc biết khoá là cách duy nhất để tạo thẻ MAC có thể xác minh.
- Symmetric: Việc tính toán và xác minh thẻ cần có cùng một khoá.
MAC có thể là cố định hoặc ngẫu nhiên, tuỳ thuộc vào thuật toán. Tink hiện không triển khai các thuật toán MAC không xác định. Bạn chỉ nên sử dụng MAC để xác thực thông điệp, chứ không phải cho các mục đích khác như tạo các byte ngẫu nhiên giả (để biết thêm thông tin, hãy xem PRF).
Nếu bạn cần một nguyên hàm bất đối xứng, hãy xem phần Chữ ký kỹ thuật số.
Chọn loại khoá
Bạn nên sử dụng HMAC_SHA256 cho hầu hết các trường hợp sử dụng, nhưng cũng có các tuỳ chọn khác.
Nói chung, những điều sau đây là đúng:
Cam kết bảo mật tối thiểu
- Độ mạnh của phương thức xác thực tối thiểu là 80 bit
- Bảo vệ khỏi hành vi giả mạo hiện hữu trong cuộc tấn công bằng văn bản thô đã chọn
- Bảo mật ít nhất 128 bit chống lại các cuộc tấn công khôi phục khoá, cũng như trong các trường hợp nhiều người dùng (khi kẻ tấn công không nhắm đến một khoá cụ thể, mà là bất kỳ khoá nào trong một tập hợp gồm tối đa 232 khoá)
Trường hợp sử dụng mẫu
Xem bài viết Tôi muốn bảo vệ dữ liệu khỏi bị can thiệp.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-25 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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)."]]