Initial MultiPay project
This commit is contained in:
commit
91abad4066
488 files changed
+76727
No files matched your search
@@ -0,0 +1,32 @@
|
||||
import { createHash, createPrivateKey, createPublicKey } from "node:crypto";
|
||||
import { mkdirSync, writeFileSync } from "node:fs";
|
||||
import { db } from "../src/db";
|
||||
import { decryptJson } from "../src/security";
|
||||
|
||||
const gatewayId = process.argv[2] || "alipay";
|
||||
const row = db.query("SELECT config_encrypted FROM gateways WHERE id=? AND channel='alipay'").get(gatewayId) as { config_encrypted: string } | null;
|
||||
if (!row) throw new Error(`找不到支付宝网关:${gatewayId}`);
|
||||
|
||||
const config = decryptJson<Record<string, string>>(row.config_encrypted);
|
||||
const raw = config.privateKey?.trim();
|
||||
if (!raw) throw new Error("网关没有配置应用私钥");
|
||||
const privatePem = raw.includes("-----BEGIN")
|
||||
? raw.replace(/\\n/g, "\n")
|
||||
: `-----BEGIN PRIVATE KEY-----\n${raw.replace(/\s+/g, "").match(/.{1,64}/g)?.join("\n")}\n-----END PRIVATE KEY-----`;
|
||||
|
||||
const privateKey = createPrivateKey(privatePem);
|
||||
const publicPem = createPublicKey(privateKey).export({ type: "spki", format: "pem" }).toString();
|
||||
const publicText = publicPem.replace(/-----BEGIN PUBLIC KEY-----|-----END PUBLIC KEY-----|\s+/g, "");
|
||||
const fingerprint = createHash("sha256").update(Buffer.from(publicText, "base64")).digest("hex");
|
||||
|
||||
mkdirSync("data", { recursive: true });
|
||||
writeFileSync("data/alipay_app_public_key.pem", publicPem, "utf8");
|
||||
writeFileSync("data/alipay_app_public_key.txt", publicText, "utf8");
|
||||
|
||||
console.log(JSON.stringify({
|
||||
gatewayId,
|
||||
appIdConfigured: Boolean(config.appId),
|
||||
pemFile: "data/alipay_app_public_key.pem",
|
||||
textFile: "data/alipay_app_public_key.txt",
|
||||
sha256Fingerprint: fingerprint
|
||||
}, null, 2));
|
||||
Reference in new issue
Block a user