Node.js 中的 XML 到 PEM [英] XML to PEM in Node.js
问题描述
因为我对密码学知之甚少,所以我一直坚持使用加密 API.我有这个
I'm stuck with the crypto api because I know little about cryptography. I have this
XML:
<RSAKeyValue>
<Modulus>1znidPBIcMcO7K/53tkTSyKqxlG5Mcws8kVtijS4tyEU4W/FEVWYpOtv+Stnb4Vt</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
Signature:
rNbdUP-p4pEGfwQSwR6VPvAVZ-sZu-ptgw8SofYYmNTlfUB9iUbb593eCAuT5jsqDTC
Original data:
<xml>...some big xml...</xml>
我想验证签名,我该怎么做?
I want to verify the signature, how do I do that?
(我使用的是节点 v0.10.18)
(I'm using node v0.10.18)
推荐答案
我不是真正的 node.js 开发人员,所以这是超级 hacky.. 这是一个应该从 Base64 模数输出 RSA 公钥 PEM 的函数和指数.我将基于 Trevor 关于 node.js verify
期待 PEM 的评论.
I'm not really a node.js dev, so this is super hacky.. Here's a function that should output a RSA public key PEM from Base64 modulus and exponent. I'm going based on Trevor's comment about node.js verify
expecting a PEM.
这个函数用十六进制组成一个ASN.1 DER结构,然后对其进行十六进制解码,然后对其进行base64编码,然后将其夹在-----BEGIN PRIVATE KEY-----
和 -----END PRIVATE KEY-----
.毕竟,这就是 PEM 的全部内容.
This function composes a ASN.1 DER structure in hex, then hex-decodes it, then base64-encodes it, and then sandwiches it between -----BEGIN PRIVATE KEY-----
and -----END PRIVATE KEY-----
. After all, that's all a PEM is.
function rsaPublicKeyPem(modulus_b64, exponent_b64) {
function prepadSigned(hexStr) {
msb = hexStr[0]
if (
(msb>='8' && msb<='9') ||
(msb>='a' && msb<='f') ||
(msb>='A'&&msb<='F')) {
return '00'+hexStr;
} else {
return hexStr;
}
}
function toHex(number) {
var nstr = number.toString(16)
if (nstr.length%2==0) return nstr
return '0'+nstr
}
// encode ASN.1 DER length field
// if <=127, short form
// if >=128, long form
function encodeLengthHex(n) {
if (n<=127) return toHex(n)
else {
n_hex = toHex(n)
length_of_length_byte = 128 + n_hex.length/2 // 0x80+numbytes
return toHex(length_of_length_byte)+n_hex
}
}
var modulus = new Buffer(modulus_b64,'base64');
var exponent = new Buffer(exponent_b64, 'base64');
var modulus_hex = modulus.toString('hex')
var exponent_hex = exponent.toString('hex')
modulus_hex = prepadSigned(modulus_hex)
exponent_hex = prepadSigned(exponent_hex)
var modlen = modulus_hex.length/2
var explen = exponent_hex.length/2
var encoded_modlen = encodeLengthHex(modlen)
var encoded_explen = encodeLengthHex(explen)
var encoded_pubkey = '30' +
encodeLengthHex(
modlen +
explen +
encoded_modlen.length/2 +
encoded_explen.length/2 + 2
) +
'02' + encoded_modlen + modulus_hex +
'02' + encoded_explen + exponent_hex;
var seq2 =
'30 0d ' +
'06 09 2a 86 48 86 f7 0d 01 01 01' +
'05 00 ' +
'03' + encodeLengthHex(encoded_pubkey.length/2 + 1) +
'00' + encoded_pubkey;
seq2 = seq2.replace(/ /g,'');
var der_hex = '30' + encodeLengthHex(seq2.length/2) + seq2;
der_hex = der_hex.replace(/ /g, '');
var der = new Buffer(der_hex, 'hex');
var der_b64 = der.toString('base64');
var pem = '-----BEGIN PUBLIC KEY-----
'
+ der_b64.match(/.{1,64}/g).join('
')
+ '
-----END PUBLIC KEY-----
';
return pem
}
使用十六进制字符串进行二进制操作?哎呀..但嘿,这是一个黑客.
Binary manipulation with hex strings? Yuck.. but hey it's a hack.
这篇关于Node.js 中的 XML 到 PEM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!