Class Poly1305
java.lang.Object
cloud.opencode.base.crypto.mac.Poly1305
- All Implemented Interfaces:
Mac
Poly1305 MAC implementation - One-time authenticator using Bouncy Castle
Poly1305 消息认证码实现 - 使用 Bouncy Castle 的一次性认证器
Features | 主要功能:
- Poly1305 message authentication code - Poly1305 消息认证码
- One-time authenticator - 一次性认证器
Usage Examples | 使用示例:
Poly1305 poly = Poly1305.create(keyBytes);
byte[] tag = poly.compute(data);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-crypto V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]compute(byte[] data) Computes MAC for the given byte array 计算给定字节数组的 MAC 值byte[]Computes MAC for the given string (UTF-8 encoded) 计算给定字符串的 MAC 值(UTF-8 编码)computeBase64(byte[] data) Computes MAC and returns as Base64 string 计算 MAC 值并返回 Base64 字符串computeHex(byte[] data) Computes MAC and returns as hexadecimal string 计算 MAC 值并返回十六进制字符串byte[]doFinal()Completes the MAC computation and returns the result 完成 MAC 计算并返回结果Completes the MAC computation and returns as Base64 string 完成 MAC 计算并返回 Base64 字符串Completes the MAC computation and returns as hexadecimal string 完成 MAC 计算并返回十六进制字符串Returns the algorithm name 返回算法名称intReturns the MAC length in bytes 返回 MAC 长度(字节数)static Poly1305of(byte[] key) Creates a new Poly1305 instance with the given key 使用给定密钥创建新的 Poly1305 实例reset()Resets the MAC to its initial state 将 MAC 重置为初始状态update(byte[] data) Updates the MAC with a chunk of data (for incremental computation) 使用数据块更新 MAC(用于增量计算)Updates the MAC with a string (UTF-8 encoded) 使用字符串更新 MAC(UTF-8 编码)booleanverify(byte[] data, byte[] macValue) Verifies MAC for the given data using constant-time comparison 使用常量时间比较验证给定数据的 MAC 值booleanverifyBase64(byte[] data, String macBase64) Verifies MAC from Base64 string using constant-time comparison 使用常量时间比较验证 Base64 字符串的 MAC 值booleanVerifies MAC from hexadecimal string using constant-time comparison 使用常量时间比较验证十六进制字符串的 MAC 值
-
Method Details
-
of
Creates a new Poly1305 instance with the given key 使用给定密钥创建新的 Poly1305 实例- Parameters:
key- the secret key bytes (must be 32 bytes)- Returns:
- new Poly1305 instance
- Throws:
NullPointerException- if key is nullOpenCryptoException- if key length is invalid or Bouncy Castle is not available
-
compute
-
compute
-
computeHex
Description copied from interface:MacComputes MAC and returns as hexadecimal string 计算 MAC 值并返回十六进制字符串- Specified by:
computeHexin interfaceMac- Parameters:
data- the data to authenticate- Returns:
- the MAC value as hex string
-
computeBase64
Description copied from interface:MacComputes MAC and returns as Base64 string 计算 MAC 值并返回 Base64 字符串- Specified by:
computeBase64in interfaceMac- Parameters:
data- the data to authenticate- Returns:
- the MAC value as Base64 string
-
verify
public boolean verify(byte[] data, byte[] macValue) Description copied from interface:MacVerifies MAC for the given data using constant-time comparison 使用常量时间比较验证给定数据的 MAC 值 -
verifyHex
Description copied from interface:MacVerifies MAC from hexadecimal string using constant-time comparison 使用常量时间比较验证十六进制字符串的 MAC 值 -
verifyBase64
Description copied from interface:MacVerifies MAC from Base64 string using constant-time comparison 使用常量时间比较验证 Base64 字符串的 MAC 值- Specified by:
verifyBase64in interfaceMac- Parameters:
data- the data to verifymacBase64- the expected MAC value in Base64 format- Returns:
- true if MAC is valid, false otherwise
-
update
-
update
-
doFinal
-
doFinalHex
Description copied from interface:MacCompletes the MAC computation and returns as hexadecimal string 完成 MAC 计算并返回十六进制字符串- Specified by:
doFinalHexin interfaceMac- Returns:
- the final MAC value as hex string
-
doFinalBase64
Description copied from interface:MacCompletes the MAC computation and returns as Base64 string 完成 MAC 计算并返回 Base64 字符串- Specified by:
doFinalBase64in interfaceMac- Returns:
- the final MAC value as Base64 string
-
reset
-
getAlgorithm
Description copied from interface:MacReturns the algorithm name 返回算法名称- Specified by:
getAlgorithmin interfaceMac- Returns:
- the algorithm name
-
getMacLength
public int getMacLength()Description copied from interface:MacReturns the MAC length in bytes 返回 MAC 长度(字节数)- Specified by:
getMacLengthin interfaceMac- Returns:
- the MAC length
-