2018-05-27 23:53:43 +01:00
|
|
|
/**
|
2018-05-28 15:42:43 -07:00
|
|
|
* Public key resources.
|
2018-05-27 23:53:43 +01:00
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 12:23:59 +01:00
|
|
|
import { toHex, fromHex } from "./Hex.mjs";
|
2018-05-27 23:53:43 +01:00
|
|
|
|
|
|
|
/**
|
2022-06-08 18:59:27 +01:00
|
|
|
* Formats Distinguished Name (DN) objects to strings.
|
2018-05-27 23:53:43 +01:00
|
|
|
*
|
2022-06-08 18:59:27 +01:00
|
|
|
* @param {Object} dnObj
|
2018-05-27 23:53:43 +01:00
|
|
|
* @param {number} indent
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-06-08 18:59:27 +01:00
|
|
|
export function formatDnObj(dnObj, indent) {
|
|
|
|
let output = "";
|
2018-05-27 23:53:43 +01:00
|
|
|
|
2022-06-08 18:59:27 +01:00
|
|
|
const maxKeyLen = dnObj.array.reduce((max, item) => {
|
|
|
|
return item[0].type.length > max ? item[0].type.length : max;
|
|
|
|
}, 0);
|
2018-05-27 23:53:43 +01:00
|
|
|
|
2022-06-08 18:59:27 +01:00
|
|
|
for (let i = 0; i < dnObj.array.length; i++) {
|
|
|
|
if (!dnObj.array[i].length) continue;
|
2018-05-27 23:53:43 +01:00
|
|
|
|
2022-06-08 18:59:27 +01:00
|
|
|
const key = dnObj.array[i][0].type;
|
|
|
|
const value = dnObj.array[i][0].value;
|
|
|
|
const str = `${key.padEnd(maxKeyLen, " ")} = ${value}\n`;
|
2018-05-27 23:53:43 +01:00
|
|
|
|
|
|
|
output += str.padStart(indent + str.length, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.slice(0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats byte strings by adding line breaks and delimiters.
|
|
|
|
*
|
|
|
|
* @param {string} byteStr
|
|
|
|
* @param {number} length - Line width
|
|
|
|
* @param {number} indent
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-02-22 19:13:38 +00:00
|
|
|
export function formatByteStr(byteStr, length, indent) {
|
2018-05-27 23:53:43 +01:00
|
|
|
byteStr = toHex(fromHex(byteStr), ":");
|
|
|
|
length = length * 3;
|
|
|
|
let output = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < byteStr.length; i += length) {
|
|
|
|
const str = byteStr.slice(i, i + length) + "\n";
|
|
|
|
if (i === 0) {
|
|
|
|
output += str;
|
|
|
|
} else {
|
|
|
|
output += str.padStart(indent + str.length, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.slice(0, output.length-1);
|
|
|
|
}
|