62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import "openpgp";
|
|
import * as openpgp from "openpgp";
|
|
import { PathLike } from "node:fs";
|
|
export function resolveChromiumPath(): string {
|
|
const chromiumPath = Deno.env.get("CHROMIUM_PATH");
|
|
if (chromiumPath) {
|
|
return chromiumPath;
|
|
}
|
|
|
|
throw new Error(
|
|
[
|
|
"CHROMIUM_PATH is not set.",
|
|
"Use the provided flake shell or set CHROMIUM_PATH manually.",
|
|
'Example: export CHROMIUM_PATH="$(command -v chromium)"',
|
|
].join("\n"),
|
|
);
|
|
}
|
|
export class OpenPGPSigner {
|
|
file_path: PathLike;
|
|
output_path: PathLike;
|
|
pri_key: Promise<openpgp.PrivateKey>;
|
|
pri_passphrase?: string;
|
|
|
|
constructor(
|
|
file: PathLike,
|
|
pri_key: PathLike,
|
|
output_path: PathLike,
|
|
pri_passphrase?: string,
|
|
) {
|
|
this.file_path = file;
|
|
this.output_path = output_path;
|
|
this.pri_passphrase = pri_passphrase;
|
|
this.pri_key = openpgp.readPrivateKey({
|
|
armoredKey: Deno.readTextFileSync(pri_key as string),
|
|
});
|
|
}
|
|
|
|
private async getDecryptedPrivateKey(): Promise<openpgp.PrivateKey> {
|
|
let privateKey = await this.pri_key;
|
|
if (this.pri_passphrase) {
|
|
privateKey = await openpgp.decryptKey({
|
|
privateKey,
|
|
passphrase: this.pri_passphrase,
|
|
});
|
|
}
|
|
return privateKey;
|
|
}
|
|
|
|
async sign() {
|
|
const file_bytes = await Deno.readFile(this.file_path as string);
|
|
const message = await openpgp.createMessage({ binary: file_bytes });
|
|
const signingKey = await this.getDecryptedPrivateKey();
|
|
const signature = await openpgp.sign({
|
|
message,
|
|
signingKeys: signingKey,
|
|
format: "armored",
|
|
detached: true,
|
|
});
|
|
await Deno.writeTextFile(this.output_path as string, signature);
|
|
}
|
|
}
|