如何在纯Node中复制openssl sha1
的功能?我已经尝试了下面的代码,但是当我运行它时得到的结果与当我运行openssl sha1
时得到的结果不同
const crypto = require('crypto');
const key = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA50usDP38lUfcpTKmiGEt
oIeUQlJbSZi8/FNshil+Wf4/oyWPtzQeZL9NoWvtDoQit5dRpRU+5c2e5j3iYlOl
JcfBX6S/HRdrJHD8xirsNRVSMq4woQbd2QcniX1fwWLhJDU8XNaTYwEGQSTnkzxA
NpvwXMixAip+Nr0YMnZOToR6gu0t0euCUWpiypr/Zm40ppeAYxZzghHcVB4zbkOB
zN0h1rIRQ6It7fv12Kd3u6semGCEtJcYBo0ZJsucRQDUpTTGzuEChI35/Q94W4QH
evQ+JD68G2jQ/eOknYHoVUdjTYh3GJaiOpu+GDJXu3GHK18JjZmM0Lsmw/Jbypfj
wwIDAQAB
-----END PUBLIC KEY-----`;
const sha1 = (str) => crypto.createHash('sha1').update(str).digest('hex');
pem.promisified.getPublicKey(key).then(({publicKey}) => {
// Remove BEGIN and END blocks, newlines
const stripped = publicKey.replace(/-----BEGIN.+KEY-----/, '').replace(/-----END.+KEY-----/,'').replaceAll('\n','');
// Remove BEGIN and END blocks only
const stripped2 = publicKey.replace(/-----BEGIN.+KEY-----/, '').replace(/-----END.+KEY-----/,'');
// Remove newlines only
const stripped3 = publicKey.replaceAll('\n','');
console.debug("SHA1: ", sha1(publicKey));
console.debug("SHA1 (stripped): ", sha1(stripped));
console.debug("SHA1 (stripped2): ", sha1(stripped2));
console.debug("SHA1 (stripped3): ", sha1(stripped3));
});
该输出:
SHA1: 2f9ab4bb70dafe6e41e401977fabea42da4c0161
SHA1 (stripped): 7cf7d0322f96221d2075123b86c5095885d5025a
SHA1 (stripped2): 58a3c4ca8e5bd33b2d2b72ea6c5bff477b743c45
SHA1 (stripped3): f80da8adcb7362fd7bd290fa6023f4b7d35a8757
通过比较,openssl sha1
计算64166b841bbb61c3f0dd7cefebb62bd79debce66
的摘要。如何在不调用openssl
二进制文件的情况下获得此SHA1?
1条答案
按热度按时间4si2a6ki1#
H/T:dave_thompson_085,因为它弄清楚了这是沿着的换行符。
问题是我的公钥没有尾随换行符。更新的草图(减去所有剥离):