@ electron/ 窗口- sign
为 Windows 编码您的应用程序 。 做为 电子 但真的支持任何带有二进制文件的文件夹 . electron-windows-sign 扫描文件夹 可签名文件 并用SHA-1和SHA-256来代号它们. 它可以高度定制化,在程序上或命令线上使用.
如果您想用EV证书或HSM设置(如DigiCert Key Locker,AWS CloudHSM,Azure Key Vault HSM,Google Cloud Key Management Service HSM等类似服务)来编码Electron或其他Windows二进制,这个工具特别有用. 关于你到底怎么做到的详情,请看 与 Cloud HSM 供应商使用 见下文.
所需资源
默认情况下,这个模块会产生 signtool.exe 并且需要运行在Windows上。 如果你正在建造一个电子应用软件 并且足够小心地给它代号 我会大力推荐你 在你建造的平台上 建造和测试你的应用软件.
使用量
Electron App的大多数开发者可能不会直接使用这个模块——而是间接使用它
换句话说 如果您是使用模块的开发者之一 @electron/forge 或者说 @electron/packager,您可以用全球环境变量配置此模块。 如果这说明
你,你可以跳过 在你的使用案例:
可在 API 文档。 。 。 .
直接使用
@electron/windows-sign 作为无害环境管理软件包分发,既可作为模块使用,也可直接从命令行使用.
import { sign } from "@electron/windows-sign"
await sign(signOptions)
electron-windows-sign $PATH_TO_APP_DIRECTORY [options ...]
有证书文件和密码
这是在Windows上对Electronapps进行编码的"传统"方式. 您通过证书文件
(像.pfx)和密码,然后传递到内置版本的 signtool.exe 带子
直接从 Windows SDK. 内置 signtool.exe 是来自Windows SDK 10.0.26100.9169的x64建筑,
符合 Azure Trusted 签名( 0.022621.755 或以后) 要求的最低版本/dlib) (中文(简体) ).
await sign({
appDirectory: "C:\\Path\\To\\App",
// or process.env.WINDOWS_CERTIFICATE_FILE
certificateFile: "C:\\Cert.pfx",
// or process.env.WINDOWS_CERTIFICATE_PASSWORD
certificatePassword: "hunter99"
})
electron-windows-sign $PATH_TO_APP_DIRECTORY --certificate-file=$PATH_TO_CERT --certificate-password=$CERT-PASSWORD
完整配置
// Path to a timestamp server. Defaults to http://timestamp.digicert.com
// Can also be passed as process.env.WINDOWS_TIMESTAMP_SERVER
timestampServer = "http://timestamp.digicert.com"
// Description of the signed content. Will be passed to signtool.exe as /d
// Can also be passed as process.env.WINDOWS_SIGN_DESCRIPTION
description = "My content"
// URL of the signed content. Will be passed to signtool.exe as /du
// Can also be passed as process.env.WINDOWS_SIGN_WEBSITE
website = "https://mywebsite.com"
// If enabled, attempt to sign .js JavaScript files. Disabled by default
signJavaScript = true
// If unspecified, both sha1 and sha256 signatures will be appended. Will be passed to signtool.exe as the /fd option.
hashes = ["sha256"]
带有自定义 signtool.exe 或自定义参数
有时,您需要指定特定的签名参数或使用不同的版本
联 合 国 signtool.exe。在此模式下, @electron/windows-sign 将调用提供的二进制
为每个文件签名提供参数.
如果你只提供 signToolPath,将使用默认参数.
如果你只提供 signWithParams默认值 signtool.exe 将被使用.
所有的东西 额外配置 上文提到的还有: 此处可使用,但仅在不提供自己的参数时使用.
await sign({
appDirectory: "C:\\Path\\To\\App",
// or process.env.WINDOWS_CERTIFICATE_FILE
certificateFile: "C:\\Cert.pfx",
// or process.env.WINDOWS_CERTIFICATE_PASSWORD
certificatePassword: "hunter99",
// or process.env.WINDOWS_SIGNTOOL_PATH
signToolPath: "C:\\Path\\To\\my-custom-tool.exe",
// or process.env.WINDOWS_SIGN_WITH_PARAMS
signWithParams: "--my=custom --parameters"
})
electron-windows-sign $PATH_TO_APP_DIRECTORY --sign-tool-path=$PATH_TO_TOOL --sign-with-params="--my=custom --parameters"
带有自定义勾钩功能
有时候,你只是希望所有模块取决于 @electron/windows-sign 调用
你的自定义逻辑。 您可以指定一个 hookFunction (如果你打电话)
此模块本身)或 a hookModulePath,该模块将试图要求.
使用 hookModulePath 好处是您可以推翻其他用户
模块( 如 @electron/packager代号您的应用程序.
await sign({
// A function with the following signature:
// (fileToSign: string) => void | Promise<void>
//
// This function will be called sequentially for each file that
// @electron/windows-sign wants to sign.
hookFunction: myHookFunction
// Path to a hook module.
hookModulePath: "C:\\Path\\To\\my-hook-module.js",
})
您的钩子模块应当直接导出一个函数或
导出a default 函数.
// Good:
module.exports = function (filePath) {
console.log(`Path to file to sign: ${filePath}`)
}
// Good:
module.exports = async function (filePath) {
console.log(`Path to file to sign: ${filePath}`)
}
// Good:
export default async function (filePath) {
console.log(`Path to file to sign: ${filePath}`)
}
// Bad:
module.exports = {
myCustomHookName: function (filePath) {
console.log(`Path to file to sign: ${filePath}`)
}
}
// Bad:
export async function myCustomHookName(filePath) {
console.log(`Path to file to sign: ${filePath}`)
}
SYNOPSIS
electron-windows-sign app [options ...]
DESCRIPTION
app
Path to the application to sign. It must be a directory.
certificate-file
Path to the certificate file (.pfx) to use for signing. Uses
environment variable WINDOWS_CERTIFICATE_FILE if not provided.
certificate-password
Password to use for the certificate. Uses environment variable
WINDOWS_CERTIFICATE_PASSWORD if not provided.
sign-tool-path
Path to the signtool.exe binary. If not specified, the tool will attempt
use a built-in version.
timestamp-server
URL of the timestamp server to use. If not specified, the tool will
attempt to use a built-in server (http://timestamp.digicert.com)
description
Description to use for the signed files. Passed as /d to signtool.exe.
website
URL of the website to use for the signed files. Passed as /du to
signtool.exe.
sign-with-params
Additional parameters to pass to signtool.exe. This can be used to
specify additional certificates to use for cross-signing.
automatically-select-certificate
Automatically select the best certificate to use for signing. On by default.
help
Print this usage information.
debug
Print additional debug information.
文件类型
此工具将积极尝试签名所有文件 能够 签名,不包括脚本.
- 可移植可执行文件 (.exe,.dll,.sys,.efi,.scr,.node) (中文(简体) )
- 微软安装器(.msi)
- APPX/MSIX包 (.appx,.appxbundle,.m6,.m6bundle) (中文(简体) )
- 目录文件 (. cat)
- 内阁档案(.cab)
- 银光应用 (.xap)
- 脚本 (.vbs,.wsf,.ps1)
如果您真的想要签名 JavaScript, 请使用 signJavaScript
参数。 据我们所知,签字没有任何好处
JavaScript 文件, 所以我们不会默认 .
与 Cloud HSM 供应商使用
2023年起,微软要求Windows软件必须用扩展验证(EV)证书来签名,以避免Windows Defender弹出. 这对习惯于在云中构建和签名的开发者或一些连续集成设置如GitHub Actions, CircleCI,Travis CI,或AppVeyor提出了挑战,因为EV证书需要硬件安全模块(HSM). 或者,用简单的词来说: 如果您想要在您的 Windows 软件上签名, 这样您的客户不会看到一个可怕的 Windows Defender 对话框, 您需要用 USB 设备插入的计算机上签名 .
业界对此问题有答案: DigiCert KeyLocker,AWS CloudHSM,Azure Key Vault HSM等服务,或Google Cloud Key Management Service HSM允许代码签名二进制,从云端获得扩展验证(EV)证书. @electron/windows-sign 符合所有这些服务.
自定义标志工具参数
大多数服务都允许与微软签署您的代码 signtool.exe。让我们以 DigiCert Key Locker 为例. DigiCert 文档 解释你安装签名机的必要步骤. 完成后,您可通过下列电话签名:
signtool.exe sign /csp "DigiCert Signing Manager KSP" /kc <keypair_alias> /f <certificate_file> /tr http://timestamp.digicert.com /td SHA256 /fd SHA256 <file_to_be_signed>
与 @electron/windows-sign 使用这些指示,你会取出参数并添加到 signWithParams数字 :
await sign({
signWithParams: "/csp \"DigiCert Signing Manager KSP\" /kc <keypair_alias> /f <certificate_file> /tr http://timestamp.digicert.com /td SHA256 /fd SHA256"
})
将证书安装到 Windows 证书商店的硬件符号和云 HSM 服务—— 如 Certum's SemplySign —— 让您通过引用证书的 SHA1 拇指印来签名 .pfx 文档。 那样的话,你不提供 certificateFile 或者说 certificatePassword ;将拇指印(和时间戳设置)通过 signWithParams数字 :
await sign({
signWithParams: "/sha1 <thumbprint> /tr http://timestamp.example /td sha256 /fd sha256"
})
Google和Amazon的解决方案同样允许您与微软的SignTool签名. 备忘表 谷歌可以在这里找到, (中文(简体) ), 亚马逊在这里的生活。 。 。 .
自定义标志工具.exe
一些供应商为以下项目提供即时替代: signtool.exe。如果是这样的话,只需将路径传递给替换者:
await sign({
// or process.env.WINDOWS_SIGNTOOL_PATH
signToolPath: "C:\\Path\\To\\my-custom-tool.exe",
})
完全自定义进程
如果您的 HSM 提供者有更复杂的设置, 您可能需要调用带有自定义参数的自定义文件 。 如果是这样的话,请使用钩子功能或钩子模块,这样您就可以完全自定义要对每个文件做什么 @electron/windows-sign 想要签名。 关于此选项的更多文档可在 上面的"带有自定义钩子功能的签名"部分。 。 。 .
许可证
BSD 2- Clause"简化". 详情请见LICENSE.