feat(electron-shell): 补录壳子源码(排除运行环境与构建产物)

This commit is contained in:
2026-09-24 16:14:24 +08:00
parent 7e0838de64
commit 3214305dea
56 changed files with 17858 additions and 0 deletions
+120
View File
@@ -0,0 +1,120 @@
// 密码学与通用工具:Ed25519 验签 / RSA-OAEP 解密 / sha256 / base64url / 版本比较。
"use strict";
const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
// ==================== base64url(无填充解码,对齐 Python urlsafe_b64decode) ====================
function b64urlDecode(s) {
// JS 中 -len % 4 为负数(Python 为正补数),显式换算补位数
const pad = (4 - (s.length % 4)) % 4;
return Buffer.from(s + "=".repeat(pad), "base64url");
}
// ==================== Ed25519 票据验签 ====================
// 票据格式:base64url(canonical_json).base64url(64B 签名);验签返回 payload 对象。
function verifyBlob(blob, publicKeyPem) {
if (typeof blob !== "string" || !blob.includes(".")) throw new Error("bad blob");
const idx = blob.lastIndexOf(".");
const b64 = blob.slice(0, idx);
const sig64 = blob.slice(idx + 1);
const canon = b64urlDecode(b64);
const sig = b64urlDecode(sig64);
if (sig.length !== 64) throw new Error("bad signature len");
const key = crypto.createPublicKey(publicKeyPem);
const ok = crypto.verify(null, canon, key, sig);
if (!ok) throw new Error("signature verification failed");
return JSON.parse(canon.toString("utf-8"));
}
// ==================== RSA-2048 密钥对(creds 下发) ====================
// 私钥 PKCS8 PEM 落盘 %LOCALAPPDATA%\37duAutomation\creds\id_rsa.pem;公钥上报中心机。
function loadOrCreateRsaKey(privPath) {
if (fs.existsSync(privPath)) {
try {
const pem = fs.readFileSync(privPath, "utf-8");
return crypto.createPrivateKey(pem);
} catch (e) {
// 损坏则重新生成
}
}
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", {
modulusLength: 2048,
publicExponent: 0x10001,
publicKeyEncoding: { type: "spki", format: "pem" },
privateKeyEncoding: { type: "pkcs8", format: "pem" },
});
fs.mkdirSync(path.dirname(privPath), { recursive: true });
fs.writeFileSync(privPath, privateKey, { encoding: "utf-8", mode: 0o600 });
return privateKey;
}
// OAEP-SHA256 解密 base64 密文 → 明文(与服务端 rsa_encrypt 对称)
function rsaDecrypt(privateKey, cipherB64) {
const ct = Buffer.from(cipherB64, "base64");
const pt = crypto.privateDecrypt(
{ key: privateKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: "sha256" },
ct
);
return pt.toString("utf-8");
}
// ==================== 通用工具 ====================
function sha256Hex(data) {
return crypto.createHash("sha256").update(data).digest("hex");
}
// 版本比较:数字段按 int,非数字段按 str 小写(对齐 Python licensing._version_lt)
function versionKey(s) {
return String(s)
.split(".")
.map((x) => (/^\d+$/.test(x) ? [1, parseInt(x, 10)] : [0, x.toLowerCase()]));
}
function versionLt(a, b) {
const ka = versionKey(a);
const kb = versionKey(b);
for (let i = 0; i < Math.max(ka.length, kb.length); i++) {
const x = ka[i] || [0, ""];
const y = kb[i] || [0, ""];
if (x[0] !== y[0]) return x[0] < y[0];
if (x[0] === 1) {
if (x[1] !== y[1]) return x[1] < y[1];
} else if (x[1] !== y[1]) return x[1] < y[1];
}
return false;
}
function deadlinePassed(deadline) {
if (!deadline) return false;
const t = Date.parse(String(deadline).replace("Z", "+00:00"));
if (Number.isNaN(t)) return false;
return Date.now() > t;
}
function nowStr() {
const d = new Date();
const p = (n) => String(n).padStart(2, "0");
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
}
function newId() {
return Date.now().toString(16) + crypto.randomBytes(4).toString("hex");
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
module.exports = {
b64urlDecode,
verifyBlob,
loadOrCreateRsaKey,
rsaDecrypt,
sha256Hex,
versionLt,
deadlinePassed,
nowStr,
newId,
sleep,
};