crypto.createMac(algorithm, key[, options])


稳定性: 1.2 - 发布候选版

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 家族必需选项可选选项备注
HMACdigest无
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.