nodejs将openpgp哈希算法转换为SHA256



我设法用OpenPGP sign((函数对消息进行了签名,但如何将哈希算法更改为SHA256?

async function pgpSignMessage(privateKey,messageToSign,callback) { 
const unsignedMessage = await openpgp.createCleartextMessage({ text: messageToSign }); 
const cleartextMessage  = await openpgp.sign({
message: unsignedMessage,  
signingKeys: privateKey,    
});

console.log("pgpSignMessage cleartextMessage ",cleartextMessage );
return callback(cleartextMessage  );
); 

输出:

pgpSignMessage cleartextMessage
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
This is a testing message
-----BEGIN PGP SIGNATURE-----
wsBzBAEBCgAGBQJjTOMPACEJEPjzUwweaxTJFiEEcJyNNiL/3PuC/Rze+PNT
DB5rFMnA2wf+OQV8+S1o12hrHgIOy362xHk7jk+bhUf2DzosyMIb5/FaP58a
Y2VxZUOMkYPFJeiJ9dO7z+25QjbUgbZz5ikExdPIIwmfY1BPX8jgWEQAniOD
ZZVg+pG2hALuZwzBchw+onvoPyyZLSNajkY4rA/wXexuAljWqWTpypoN51iQ
O/E4TN2FbAIQOq89tzS5MXhEAZe4bYHH4cUwsQiaIjA+2OjrspPJt+3/nosP
1f6k6wgZKuPDbyK6KKSqtb0tzFmIVkF+4+VF+vQFi7kx5utWn/nXOWO78tUV
s6dn23+GdJ5sLaFUT5vPDKux3dddErPOKEam/hJUe5VoO2LzeNO5Ww==
=KsDz
-----END PGP SIGNATURE-----

访问openpgp.js源(在本例中是最新的master提交(,并向下查看sign()函数代码,您偶尔会看到以下内容:

export async function getPreferredHashAlgo(key, keyPacket, date = new Date(), userID = {}, config) {
let hashAlgo = config.preferredHashAlgorithm;
let prefAlgo = hashAlgo;
if (key) {
const primaryUser = await key.getPrimaryUser(date, userID, config);
if (primaryUser.selfCertification.preferredHashAlgorithms) {
[prefAlgo] = primaryUser.selfCertification.preferredHashAlgorithms;
hashAlgo = crypto.hash.getHashByteLength(hashAlgo) <= crypto.hash.getHashByteLength(prefAlgo) ?
prefAlgo : hashAlgo;
}
}
...

因此,它将首先从config.preferredHashAlgorithm中选取值。然后,如果主用户的id证书包含首选哈希算法子包,并且第一个值的哈希长度比配置的哈希长度长,它就会拾取它。

您可以通过gpg --list-packets命令获得有关密钥的更多信息。

相关内容

  • 没有找到相关文章