导入密钥
🌐 Importing keys
在为重复使用创建 KeyObject 时,导入成本只需支付一次,因此选择更快的格式可以减少启动延迟。
🌐 When creating a KeyObject for repeated use, the import cost is paid once,
so choosing a faster format reduces startup latency.
导入成本分为两部分:解析开销(解码序列化封装)和密钥计算(重建完整密钥所需的任何数学工作,例如从私有标量导出公钥或扩展种子)。哪一部分占主导取决于密钥类型。例如:
🌐 The import cost breaks down into two parts: parsing overhead (decoding the serialization wrapper) and key computation (any mathematical work needed to reconstruct the full key, such as deriving a public key from a private scalar or expanding a seed). Which part dominates depends on the key type. For example:
- 公钥 -
'raw-public'是最快的序列化格式,因为原始格式跳过了所有 ASN.1 和 Base64 解码。 - EC 私钥 -
'raw-private'比 PEM 或 DER 更快,因为它避免了 ASN.1 解析。然而,对于较大的曲线(例如 P-384、P-521),从私有标量推导公点所需的计算变得很昂贵,从而降低了优势。 - RSA 密钥 -
'jwk'是最快的序列化格式。JWK 将 RSA 密钥组件表示为单独的 Base64url 编码的整数,从而完全避免了 ASN.1 解析的开销。