Skip to content

Instantly share code, notes, and snippets.

@gbaranski
Last active December 17, 2020 20:13
Show Gist options
  • Save gbaranski/733e5ddba8a5d3e56a476ba96264fdce to your computer and use it in GitHub Desktop.
Save gbaranski/733e5ddba8a5d3e56a476ba96264fdce to your computer and use it in GitHub Desktop.
PEM Certificate parser for C/C++
deno run --allow-read \
  https://gist.githubusercontent.com/gbaranski/733e5ddba8a5d3e56a476ba96264fdce/raw/f22038fcb3f04ab59ef34a1a336e4c21236c0287/pem_parser.ts \
  <path-to-cert>
import { red, green, magenta } from "https://deno.land/std/fmt/colors.ts";
const certPath = Deno.args[0];
if (!certPath)
throw new Error(
red(
`You need to pass first argument to file, eg ${magenta(
"deno run index.ts cert.pem"
)}`
)
);
const parse = (e: string) => `\t"${e}\\n"`; // \t is needed only for parsing it to
const parseLastLine = (e: string) => `\t"${e}"`;
const text = await Deno.readTextFile(certPath);
const parsed = text
.trim() // Remove whitespaces
.split("\n") // Split by newline
.map((e, i, a) => (i + 1 === a.length ? parseLastLine(e) : parse(e))) // Parse it
.join("\n"); // Join all together
const code = `
${magenta(`const char *${red("certificate")} =`)}
${green(parsed)};
`;
console.log(code);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment