format-key
Version:
34 lines (24 loc) • 678 B
JavaScript
const stdin = process.stdin;
if (stdin.isTTY) {
throw new Error('Standard input required');
process.exit(1)
}
let key = '';
stdin.setEncoding('utf-8');
stdin.on('readable', () => {
let chunk;
while((chunk = stdin.read())) {
key += chunk;
}
});
stdin.on('end', () => {
key = key.replace(/\n/g,'');
const [header, body, footer] = key
.replace(/(-----BEGIN\s(RSA\s)?(PUBLIC|PRIVATE)\sKEY-----)/, '$1\n')
.replace(/(-----END\s(RSA\s)?(PUBLIC|PRIVATE)\sKEY-----)/, '\n$1')
.split('\n')
.filter(key => key !== '')
const result = [header, body.replace(/(.{64})/g, '$1\n'), footer].join('\n');
console.log(result);
});