我签署的文件是这样的。
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="CDA_PL_IG_1.3.1.xsl" type="text/xsl"?>
<ClinicalDocument></ClinicalDocument>
我正在使用xadesjs
以下代码对此 XML 进行签名:
const crypto = new Crypto();
xadesjs.Application.setEngine('NodeJS', crypto);
export async function sign(xml: string, { publicKey, privateKey }: any) {
const hash = 'SHA-1';
const alg = {
name: 'RSASSA-PKCS1-v1_5',
hash
};
const keyDer = pem2der(privateKey.toString());
const key = await crypto.subtle.importKey('pkcs8', keyDer, alg, true, [ 'sign' ]);
const parsed = xadesjs.Parse(xml.trim());
const xadesXml = new xadesjs.SignedXml();
const signature = await xadesXml.Sign(alg, key, parsed, {
signingCertificate: preparePem(publicKey.toString()),
references: [ { uri: '', hash, transforms: [ 'enveloped' ] } ],
x509: [ preparePem(publicKey.toString()) ]
});
parsed.documentElement.appendChild(signature.GetXml()!);
return parsed.toString();
}
function preparePem(pem: string) {
return pem.replace(/-----(BEGIN|END)[\w\d\s]+-----/g, '').replace(/[\r\n]/g, '');
}
function pem2der(pem: string) {
pem = preparePem(pem);
return new Uint8Array(Buffer.from(pem, 'base64')).buffer;
}
仅当我删除 xml 声明和样式表指令时,生成的签名才有效。因此,仅签署此文件会返回正确签署的文档:
<ClinicalDocument></ClinicalDocument>
签署这个
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="CDA_PL_IG_1.3.1.xsl" type="text/xsl"?>
<ClinicalDocument></ClinicalDocument>
错误消息说不是整个文档都已签名。
我认为问题出在URI=""
参考上。它只签名并<ClinicalDocument>
留下不签名。<?xml version>
<?xml-stylesheet>
我如何签署所有内容?