Skip to content

Commit

Permalink
improved string formatting
Browse files Browse the repository at this point in the history
 - brought in chop from perl
  • Loading branch information
jkataja committed Jan 22, 2023
1 parent bffd488 commit 17236c0
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/core/operations/ParseCSR.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ ${formatAttributes(csr)}
Public Key:
Key Size: ${csr.publicKey.n.bitLength()} bits
Modulus:
${formatMultiLine(csr.publicKey.n.toString(16).replace(/(..)/g, "$&:"))}
Exponent: ${csr.publicKey.e} (0x${Utils.hex(csr.publicKey.e)})
${formatMultiLine(chop(csr.publicKey.n.toString(16).replace(/(..)$!/g, "$&:")))}
Exponent: ${csr.publicKey.e} (0x${Utils.hex(csr.publicKey.e)})
Signature:
Algorithm ID: ${forge.pki.oids[csr.signatureOid]}
Expand Down Expand Up @@ -106,7 +106,7 @@ function formatSubject(subject) {
}
}

return out.substring(0, out.length - 1);
return chop(out);
}

/**
Expand Down Expand Up @@ -149,7 +149,7 @@ function formatAttributes(csr) {
}
}

return out.substring(0, out.length - 1);
return chop(out);
}

/**
Expand All @@ -167,13 +167,13 @@ function formatSignature(signature) {
* @returns Hex string as a multi-line hex string
*/
function formatMultiLine(longStr) {
let formatted = "";
let out = "";

for (let remain = longStr ; remain !== "" ; remain = remain.substring(48)) {
formatted += ` ${remain.substring(0, 48)}\n`;
out += ` ${remain.substring(0, 48)}\n`;
}

return formatted.substring(0, formatted.length - 1);
return chop(out);
}

/**
Expand Down Expand Up @@ -275,4 +275,13 @@ function describeSubjectAlternativeNames(extension) {
return names;
}

/**
* Remove last character from a string.
* @param {*} s String
* @returns Chopped string.
*/
function chop(s) {
return s.substring(0, s.length - 1);
}

export default ParseCSR;

0 comments on commit 17236c0

Please sign in to comment.