codesign 是 macOS 和 iOS 平台上用于数字签名应用、命令行工具、动态库、插件等可执行文件的系统命令行工具。
它是 Apple 安全体系(App Sandbox、Gatekeeper、Notarization、App Store 等)的核心之一,确保应用来源可信、未被篡改。
codesign 是什么?
codesign
是 macOS 系统自带的命令行工具。
属于 Xcode 的一部分(也可系统使用)。
用于给可执行文件打上「身份标识」的数字签名。
开发者必须使用它来签名 App,特别是在发布、测试、调试时。
主要用途
1、应用签名:发布 iOS/macOS 应用(Xcode 内部调用)。
2、开发调试签名:本地运行未发布应用时必须签名。
3、安全验证:用户运行 App 前系统会验证签名(Gatekeeper)。
4、Notarization 签名:要发布给非 App Store 用户的 macOS App。
5、插件/工具签名:.dylib、.framework、.kext、.pkg 等。
为什么需要签名?
macOS 有严格的安全策略:
1、Gatekeeper(防止用户安装不明来源 App)。
2、App Sandbox(隔离 App 权限)。
3、Notarization(App 分发前提交 Apple 检查)。
如果不签名:
1、App 可能无法运行(被系统阻止)。
2、iOS App 绝对无法安装到真机或发布。
3、macOS 用户会收到「开发者身份无法验证」错误。
常用命令
1、查看某个 App 是否已签名
codesign -dvvv /Applications/MyApp.app
输出信息包括:签名证书名称、Team ID、Entitlements和代码哈希等信息。
2、给可执行文件签名(开发测试)
codesign -s "Apple Development: John Appleseed (TEAMID)" /path/to/MyTool
参数说明:
-s:签名证书名称(可以用 security find-identity 查看可用证书)。
文件可以是 .app、.dylib、.command、.framework 等。
3、移除签名(只在测试中用)
codesign --remove-signature /path/to/file
4、验证签名是否有效
codesign --verify --deep --strict --verbose=4 /path/to/MyApp.app
常用参数
1、-s:指定签名证书。
2、–entitlements:指定权限文件。
3、–deep:递归签名 App 内嵌的子组件(如 frameworks)。
4、–force:强制覆盖旧签名。
5、–verify:验证签名是否有效。
6、–remove-signature:移除已有签名。
签名过程背后的原理
签名会做以下几件事:
1、生成 SHA 哈希摘要,记录文件结构。
2、使用证书中的私钥加密该摘要。
3、将证书链 + 加密信息打包附在可执行文件中。
4、用户运行 App 时,系统会用公钥验证这些签名是否合法、完整、是否被篡改。
获取签名证书
在 Xcode 中:
打开 Settings… > Accounts 登录 Apple ID。
点击“Manage Certificates”。

创建 Apple Development / Apple Distribution 证书。

或者命令行使用:
security find-identity -v -p codesigning
总结
codesign常用于本地调试App、真机运行iOS App、macOS App分发给别人、AppStore发布等场景。
扩展知识
1、签名信息
使用codesign -dvvv app,获取应用的签名信息:
fangjunyu@MacBook-Air ~ % codesign -dvvv /Applications/ImageSlim.app
Executable=/Applications/ImageSlim.app/Contents/MacOS/ImageSlim
Identifier=com.fangjunyu.ImageSlim
Format=app bundle with Mach-O thin (arm64)
CodeDirectory v=20500 size=675 flags=0x10000(runtime) hashes=10+7 location=embedded
Hash type=sha256 size=32
CandidateCDHash sha256=ed30e11ced05f01077dccfd2c8ac490561e0551e
CandidateCDHashFull sha256=ed30e11ced05f01077dccfd2c8ac490561e0551e95bd5691a4813d82037500a6
Hash choices=sha256
CMSDigest=ed30e11ced05f01077dccfd2c8ac490561e0551e95bd5691a4813d82037500a6
CMSDigestType=2
CDHash=ed30e11ced05f01077dccfd2c8ac490561e0551e
Signature size=4777
Authority=Apple Development: junyu fang (DGB6H35P5R)
Authority=Apple Worldwide Developer Relations Certification Authority
Authority=Apple Root CA
Signed Time=Jun 29, 2025 at 13:54:00
Info.plist entries=24
TeamIdentifier=RRMSD743UQ
Runtime Version=15.4.0
Sealed Resources version=2 rules=13 files=58
Internal requirements count=1 size=184
1、包ID:
App 的 Bundle Identifier,来自Info.plist中的CFBundleIdentifier。
Identifier=com.fangjunyu.ImageSlim
2、架构信息:
Format=app bundle with Mach-O thin (arm64)
表示这是标准的 App Bundle,架构是 arm64(Apple Silicon)。
3、签名目录信息:
CodeDirectory v=20500 size=675 flags=0x10000(runtime) hashes=10+7 location=embedded
标明使用了 Runtime Entitlements 签名。
Hash type=sha256 size=32
CandidateCDHash sha256=ed30e11c...a6
CDHash=ed30e11c...a6
应用签名时用的哈希摘要(CDHash 是核心校验标识)。
这些是 Apple 用来校验代码完整性的。
4、签名证书链(Authority)
Authority=Apple Development: junyu fang (DGB6H35P5R)
Authority=Apple Worldwide Developer Relations Certification Authority
Authority=Apple Root CA
这是使用的完整证书链,说明:
用的是开发证书签名(不是发布)。
系统信任的证书路径完整。
DGB6H35P5R 是个人开发证书的 ID。
5、签名时间
Signed Time=Jun 29, 2025 at 13:54:00
表示这次签名发生在 2025 年 6 月 29 日下午。
6、其他信息
TeamIdentifier=RRMSD743UQ
Apple Developer 团队 ID(如果上传 App,会用这个判断归属)。
Runtime Version=15.4.0
macOS 系统 Runtime(或使用的 SDK runtime)。
Sealed Resources version=2 rules=13 files=58
App bundle 中的资源文件(图标、XIB、图片等)也已被签名校验,防篡改。