xml-crypto icon indicating copy to clipboard operation
xml-crypto copied to clipboard

how to add `Id` to KeyInfo tag ?

Open sibelius opened this issue 11 months ago • 3 comments

<ds:KeyInfo Id="key-info-id">

sibelius avatar Jan 28 '25 20:01 sibelius

Are you aware that this repository has discussions section activated?

IMHO all of these could have been started as questions at discussions instead of issues:

  • https://github.com/node-saml/xml-crypto/issues/479
  • https://github.com/node-saml/xml-crypto/issues/480
  • https://github.com/node-saml/xml-crypto/issues/481
  • https://github.com/node-saml/xml-crypto/issues/482
  • https://github.com/node-saml/xml-crypto/issues/483

Some of those issues were resolved by you with comment like "nevermind"/"got working". If you had an issue why not share solution with others who might have tried to already spend time to replicate your problem with short info that you provided.

srd90 avatar Jan 30 '25 08:01 srd90

both closed issues answered with more info to help people

sibelius avatar Jan 30 '25 20:01 sibelius

IMHO this issue can be closed because this is actually "part of" problem described at issue:

  • https://github.com/node-saml/xml-crypto/issues/481

which in turn is duplicate of

  • https://github.com/node-saml/xml-crypto/issues/463
  • which is being handled/WIP at https://github.com/node-saml/xml-crypto/pull/464

srd90 avatar Feb 17 '25 17:02 srd90

Hi @sibelius, have you ever succeeded in doing this?

I'm having the same problem with Signed XML.

matheusinfo avatar Apr 30 '25 00:04 matheusinfo

Not with nodejs, we moved to Java for this

sibelius avatar Apr 30 '25 00:04 sibelius

@sibelius @matheusinfo Has two way to achieve this. First one (Via Constructor Option)

const SignedXml = require("xml-crypto").SignedXml;
const fs = require("fs");

const xml = "<root><item>Some content</item></root>";

const sig = new SignedXml({
  privateKey: fs.readFileSync("path/to/your/privateKey.pem"),
  publicCert: fs.readFileSync("path/to/your/publicCert.pem"),
  keyInfoAttributes: { Id: "key-info-id" } // Add your Id attribute here
});

// Add references, etc.
sig.addReference({
  xpath: "//*[local-name(.)='item']"
});

// Compute the signature
// If you want the "ds" prefix for KeyInfo (as in <ds:KeyInfo>),
// provide it in the computeSignature options.
sig.computeSignature(xml, {
  prefix: "ds" // This will make the KeyInfo tag <ds:KeyInfo>
});

const signedXml = sig.getSignedXml();
console.log(signedXml);

Second one (Via Instance Property)

const SignedXml = require("xml-crypto").SignedXml;
const fs = require("fs");

const xml = "<root><item>Some content</item></root>";

const sig = new SignedXml({
  privateKey: fs.readFileSync("path/to/your/privateKey.pem"),
  publicCert: fs.readFileSync("path/to/your/publicCert.pem")
});

sig.keyInfoAttributes = { Id: "key-info-id" }; // Set your Id attribute here

sig.addReference({
  xpath: "//*[local-name(.)='item']"
});

sig.computeSignature(xml, {
  prefix: "ds"
});

const signedXml = sig.getSignedXml();
console.log(signedXml);

jlucaso1 avatar May 09 '25 21:05 jlucaso1