import { fromBase64, toBase64, toHex } from "../crypto";
const ARMOR = /-----BEGIN PGP (PUBLIC KEY BLOCK|SIGNATURE)-----\r?\n([\s\S]*?)-----END PGP \1-----/;
const TAG_SIGNATURE = 2;
const TAG_PUBLIC_KEY = 6;
const TAG_PUBLIC_SUBKEY = 14;
const SUBPACKET_ISSUER = 16;
const SUBPACKET_ISSUER_FINGERPRINT = 33;
const SIG_BINARY = 0x00;
const SIG_TEXT = 0x01;
const ALGO_RSA = [1, 3];
const ALGO_ECDSA = 19;
const ALGO_EDDSA_LEGACY = 22;
const ALGO_ED25519 = 27;
const ED25519_BYTES = 32;
const PGP_V4 = 4;
const TWO_OCTET_START = 192;
const PARTIAL_START = 224;
const FOUR_OCTET = 255;
const NATIVE_POINT_PREFIX = 0x40;
const UNCOMPRESSED_POINT = 4;
const HASHES: Record<number, string> = { 2: "SHA-1", 8: "SHA-256", 9: "SHA-384", 10: "SHA-512" };
const ED25519_OID = "2b06010401da470f01";
const CURVES: Record<string, { curve: "P-256" | "P-384" | "P-521"; size: number }> = {
"2a8648ce3d030107": { curve: "P-256", size: 32 },
"2b81040022": { curve: "P-384", size: 48 },
"2b81040023": { curve: "P-521", size: 66 },
};
type Packet = { tag: number; body: Uint8Array };
export function dearmor(text: string): Uint8Array<ArrayBuffer> | null {
const match = ARMOR.exec(text.replace(/\r\n/g, "\n"));
if (!match) return null;
const lines = match[2].split("\n");
const blank = lines.findIndex((line) => line.trim() === "");
const body = (blank === -1 ? lines : lines.slice(blank + 1)).filter((line) => !line.startsWith("="));
return fromBase64(body.join("").replace(/\s+/g, ""));
}