crypto.createMac(algorithm, key[, options])
algorithm<string> MAC 算法的名称。key<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <KeyObject>options<Object>stream.transform选项digest<string> MAC(如 HMAC)使用的摘要。cipher<string> MAC(如 CMAC 或 GMAC)使用的密码算法。iv<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 用于 MAC(如 GMAC)的初始化向量。customization<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 一个针对支持它的 MAC(如 KMAC)的自定义字节串。salt<ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 用于支持它的 MAC(如 BLAKE2 MAC)的盐字节串。outputLength<number> 请求的提供者输出大小(以字节为单位)。必须是无符号的 32 位整数。也适用特定提供者的限制。
- 返回:Mac
algorithm 必须是非空的提供者 MAC 名称。上述列出的与 MAC 相关的属性是对标准 stream.transform 选项 的扩展,并且仅在所选提供者实现以预期类型发布相应参数时才会传递。如果提供了所选实现不支持的 MAC 特定选项,会导致错误。
下表总结了 OpenSSL 内置提供者中 MAC 实现所接受的特定 MAC 选项。每个 MAC 都需要 key 参数。表中只列出了特定 MAC 的选项;标准的 stream.transform 选项 对每个系列仍然可用。
🌐 The following table summarizes the MAC-specific options accepted by MAC
implementations in OpenSSL's built-in providers. The key argument is required
for every MAC. The table lists only MAC-specific options; standard
stream.transform options remain available for every family.
| MAC 家族 | 必需选项 | 可选选项 | 备注 |
|---|---|---|---|
| HMAC | digest | 无 | |
| CMAC | 使用 CBC 模式的 cipher | 无 | |
| GMAC | 使用 GCM 模式的 cipher,非空 iv | 无 | 对于每个使用给定密钥进行认证的消息,都需要唯一的 IV。 |
| KMAC | 无 | customization,outputLength | |
| BLAKE2 MAC | 无 | customization,salt,outputLength | |
| Poly1305 | 无 | 无 | 每个密钥只能用于一条消息。 |
| SipHash | 无 | outputLength |
outputLength 配置了提供者 MAC 的输出大小。它从不通过计算更长的标签然后截断来实现。0 的值会传递给提供者,并且只有当该提供者能够用零字节输出初始化和结束 MAC 时才被接受。当省略 outputLength 时,将使用提供者的默认输出大小,并且必须非零。
key 必须包含字节,或者是类型为 secret 的 KeyObject。密钥长度和其他密钥要求由所选提供者实现决定。
🌐 The key must contain bytes or be a KeyObject of type secret. Key
length and other key requirements are determined by the selected provider
implementation.
可用的算法及其可接受的参数取决于 OpenSSL 版本、已加载的提供者以及当前活动的默认属性查询。使用 crypto.getMacs() 列出可获取的 MAC 名称。列出的名称仍然可能需要带有提供者特定属性的选项或密钥。
🌐 Available algorithms and their accepted parameters depend on the OpenSSL
version, loaded providers, and active default property query. Use
crypto.getMacs() to list fetchable MAC names. A listed name can still
require options or a key with provider-specific properties.