(node.js)创建具有自动JSON Canonization的埃及ITIDA CADES-BES签名

发布于 2025-02-05 05:07:41 字数 2571 浏览 2 评论 0原文

我正在使用一个示例(node.js创建埃及itida cades-bes signature具有自动json canonicalization),但我始终会遇到此错误( 4043 4043:message-digigest属性属性值不匹配计算值[message-digest属性值不匹配计算值] )。

您能帮我解决方案吗?

使用的代码:

 // This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

var crypt = new chilkat.Crypt2();
crypt.VerboseLogging = true;

var cert = new chilkat.Cert();
cert.VerboseLogging = true;

// Set the smart card PIN, which will be needed for signing.
cert.SmartCardPin = "12345678";

// There are many ways to load the certificate.  
// This example was created for a customer using an ePass2003 USB token.
// Assuming the USB token is the only source of a hardware-based private key..
var success = cert.LoadFromSmartcard("");
if (success !== true) {
    console.log(cert.LastErrorText);
    return;
}

// Tell the crypt class to use this cert.
success = crypt.SetSigningCert(cert);
if (success !== true) {
    console.log(crypt.LastErrorText);
    return;
}

var cmsOptions = new chilkat.JsonObject();
// Setting "DigestData" causes OID 1.2.840.113549.1.7.5 (digestData) to be used.
cmsOptions.UpdateBool("DigestData",true);
cmsOptions.UpdateBool("OmitAlgorithmIdNull",true);

// Indicate that we are passing normal JSON and we want Chilkat do automatically
// do the ITIDA JSON canonicalization:
cmsOptions.UpdateBool("CanonicalizeITIDA",true);

crypt.CmsOptions = cmsOptions.Emit();

// The CadesEnabled property applies to all methods that create CMS/PKCS7 signatures. 
// To create a CAdES-BES signature, set this property equal to true. 
crypt.CadesEnabled = true;

crypt.HashAlgorithm = "sha256";

var jsonSigningAttrs = new chilkat.JsonObject();
jsonSigningAttrs.UpdateInt("contentType",1);
jsonSigningAttrs.UpdateInt("signingTime",1);
jsonSigningAttrs.UpdateInt("messageDigest",1);
jsonSigningAttrs.UpdateInt("signingCertificateV2",1);
crypt.SigningAttributes = jsonSigningAttrs.Emit();

// By default, all the certs in the chain of authentication are included in the signature.
// If desired, we can choose to only include the signing certificate:
crypt.IncludeCertChain = false;


var jsonToSign = "{ ... }";

// Create the CAdES-BES signature.
crypt.EncodingMode = "base64";

// Make sure we sign the utf-8 byte representation of the JSON string
crypt.Charset = "utf-8";

var sigBase64 = crypt.SignStringENC(jsonToSign);
if (crypt.LastMethodSuccess == false) {
    console.log(crypt.LastErrorText);
    return;
}

console.log("Base64 signature:");
console.log(sigBase64);

I am using an example (Node.js Create Egypt ITIDA CAdES-BES Signature with Automatic JSON Canonicalization) but I always get this error ( 4043 4043:message-digest attribute value does not match calculated value[message-digest attribute value does not match calculated value] ).

Can you help me with the solution?

Code Used:

 // This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

var crypt = new chilkat.Crypt2();
crypt.VerboseLogging = true;

var cert = new chilkat.Cert();
cert.VerboseLogging = true;

// Set the smart card PIN, which will be needed for signing.
cert.SmartCardPin = "12345678";

// There are many ways to load the certificate.  
// This example was created for a customer using an ePass2003 USB token.
// Assuming the USB token is the only source of a hardware-based private key..
var success = cert.LoadFromSmartcard("");
if (success !== true) {
    console.log(cert.LastErrorText);
    return;
}

// Tell the crypt class to use this cert.
success = crypt.SetSigningCert(cert);
if (success !== true) {
    console.log(crypt.LastErrorText);
    return;
}

var cmsOptions = new chilkat.JsonObject();
// Setting "DigestData" causes OID 1.2.840.113549.1.7.5 (digestData) to be used.
cmsOptions.UpdateBool("DigestData",true);
cmsOptions.UpdateBool("OmitAlgorithmIdNull",true);

// Indicate that we are passing normal JSON and we want Chilkat do automatically
// do the ITIDA JSON canonicalization:
cmsOptions.UpdateBool("CanonicalizeITIDA",true);

crypt.CmsOptions = cmsOptions.Emit();

// The CadesEnabled property applies to all methods that create CMS/PKCS7 signatures. 
// To create a CAdES-BES signature, set this property equal to true. 
crypt.CadesEnabled = true;

crypt.HashAlgorithm = "sha256";

var jsonSigningAttrs = new chilkat.JsonObject();
jsonSigningAttrs.UpdateInt("contentType",1);
jsonSigningAttrs.UpdateInt("signingTime",1);
jsonSigningAttrs.UpdateInt("messageDigest",1);
jsonSigningAttrs.UpdateInt("signingCertificateV2",1);
crypt.SigningAttributes = jsonSigningAttrs.Emit();

// By default, all the certs in the chain of authentication are included in the signature.
// If desired, we can choose to only include the signing certificate:
crypt.IncludeCertChain = false;


var jsonToSign = "{ ... }";

// Create the CAdES-BES signature.
crypt.EncodingMode = "base64";

// Make sure we sign the utf-8 byte representation of the JSON string
crypt.Charset = "utf-8";

var sigBase64 = crypt.SignStringENC(jsonToSign);
if (crypt.LastMethodSuccess == false) {
    console.log(crypt.LastErrorText);
    return;
}

console.log("Base64 signature:");
console.log(sigBase64);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稳稳的幸福 2025-02-12 05:07:42

我们遇到了这个错误,直到我们被告知不要在JSON文件中使用任何空值。因此,请尝试用“”替换JSON文件中的任何空值。

We were having this error, until we were advised of not using any null values in the json file. So, pls try to replace any null values in json file with "".

失与倦" 2025-02-12 05:07:42

有关调试的详细信息以及您可以发送到chilkat的详细信息: .example-code.com/nodejs/itida_egypt_debug.asp

See this example for details about debugging and what you can send to Chilkat: https://www.example-code.com/nodejs/itida_egypt_debug.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文