{"id":32317311,"url":"https://github.com/muzipiao/gmobjc","last_synced_at":"2026-02-20T23:02:07.579Z","repository":{"id":41271477,"uuid":"200012539","full_name":"muzipiao/GMObjC","owner":"muzipiao","description":"SM2/SM3/SM4/ECDH crypto library based on OpenSSL.","archived":false,"fork":false,"pushed_at":"2025-10-24T04:05:10.000Z","size":109470,"stargazers_count":385,"open_issues_count":1,"forks_count":101,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-12-05T03:03:59.859Z","etag":null,"topics":["asn1","crypto","ecc","ecdh","guomi","hmac","sm2","sm3","sm4"],"latest_commit_sha":null,"homepage":"https://muzipiao.github.io/gmdocs/","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/muzipiao.png","metadata":{"files":{"readme":"README-CN.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-01T08:43:24.000Z","updated_at":"2025-12-03T09:43:24.000Z","dependencies_parsed_at":"2023-11-14T12:26:51.924Z","dependency_job_id":"6bb87023-98ff-4eba-928c-494efeec7fa4","html_url":"https://github.com/muzipiao/GMObjC","commit_stats":{"total_commits":84,"total_committers":3,"mean_commits":28.0,"dds":"0.023809523809523836","last_synced_commit":"2e9f64e952456dd43845aef1900308aef7ad46c1"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"purl":"pkg:github/muzipiao/GMObjC","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muzipiao%2FGMObjC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muzipiao%2FGMObjC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muzipiao%2FGMObjC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muzipiao%2FGMObjC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/muzipiao","download_url":"https://codeload.github.com/muzipiao/GMObjC/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muzipiao%2FGMObjC/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29667119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-20T19:49:36.704Z","status":"ssl_error","status_checked_at":"2026-02-20T19:44:05.372Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asn1","crypto","ecc","ecdh","guomi","hmac","sm2","sm3","sm4"],"created_at":"2025-10-23T11:06:53.139Z","updated_at":"2026-02-20T23:02:07.562Z","avatar_url":"https://github.com/muzipiao.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://muzipiao.github.io/gmdocs/zh/\"\u003e\n    \u003cimg src=\"https://muzipiao.github.io/gmdocs/img/gmobjc-logo-rect.svg\" width=\"50%\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![English README](https://img.shields.io/badge/%F0%9F%91%89%20English%20README%F0%9F%91%88-8A2BE2)](https://github.com/muzipiao/GMObjC/blob/master/README.md)\n[![Build Status](https://github.com/muzipiao/GMObjC/actions/workflows/build.yml/badge.svg)](https://github.com/muzipiao/GMObjC/actions/workflows/build.yml)\n[![Pod Version](https://img.shields.io/cocoapods/v/GMObjC.svg?style=flat)](https://cocoapods.org/pods/GMObjC)\n[![Platforms](https://img.shields.io/cocoapods/p/GMObjC.svg?style=flat)](https://cocoapods.org/pods/GMObjC)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-brightgreen.svg)](https://github.com/muzipiao/GMObjC)\n[![SwiftPM compatible](https://img.shields.io/badge/SwiftPM-compatible-brightgreen.svg)](https://swift.org/package-manager/)\n[![codecov](https://codecov.io/gh/muzipiao/GMObjC/branch/master/graph/badge.svg)](https://codecov.io/gh/muzipiao/GMObjC)\n\n\u003c/div\u003e\n\n**GMObjC** 是一个基于 OpenSSL 的国密（SM2、SM3、SM4）算法的 Objective-C 开源库，适用于 iOS 和 macOS 开发。它封装了中国国家密码管理局发布的多种加密算法，包括：\n\n- **SM2**：支持基于椭圆曲线（ECC）的加解密，密钥协商（ECDH）和签名算法。\n- **SM3**：类似 SHA 系列的国密哈希算法，包含 SM3 和 HMAC 等。\n- **SM4**：实现对称分组加密算法。\n\n## 文档\n\n要查看详细文档，请访问 [https://muzipiao.github.io/gmdocs/zh/](https://muzipiao.github.io/gmdocs/zh/)。\n\n## 尝试 Demo\n\n在终端运行以下命令:\n\n```ruby\n# 国内用户可访问 git clone https://gitee.com/cocoafei/GMObjC.git\ngit clone https://github.com/muzipiao/GMObjC.git\n\ncd GMObjC\n\npod install\n\nopen GMObjC.xcworkspace\n```\n\n## 安装方式\n\n安装方式参考文档 **[GMObjC 详细安装说明: https://muzipiao.github.io/gmdocs/zh/guide/getting-started](https://muzipiao.github.io/gmdocs/zh/guide/getting-started)** \n\n- **[使用 CocoaPods 安装](https://muzipiao.github.io/gmdocs/zh/guide/getting-started#install-cocoapods)**：在 Podfile 中添加`pod 'GMObjC'`或`pod 'GMDynamic'`快速集成。\n- **[使用 Carthage 安装](https://muzipiao.github.io/gmdocs/zh/guide/getting-started#install-carthage)**：使用 Carthage 编译为 **GMObjC.xcframework** 动态库。\n- **[使用 Framework 安装](https://muzipiao.github.io/gmdocs/zh/guide/getting-started#install-framework)**：打开项目 **GMObjC.xcodeproj** 手动编译为动态库或静态库。\n- **[使用 SPM 安装](https://muzipiao.github.io/gmdocs/zh/guide/getting-started#install-spm)**：在项目 **Package.swift** 中添加依赖集成。\n- **[源码直接集成](https://muzipiao.github.io/gmdocs/zh/guide/getting-started#install-source)**：将项目目录下的 **GMObjC** 文件夹直接拖入项目使用。\n\n## SM2 密钥对\n\n```objc\n// 生成公私钥，公私钥都为 HEX 编码的字符串格式\nGMSm2Key *keyPair = [GMSm2Utils generateKey];\n// SM2 公钥 \"0408E3FFF9505BCFAF9307E665...695C99DF42424F28E9004CDE4678F63D698\"\nNSString *pubKey = keyPair.publicKey;\n// SM2 私钥 \"90F3A42B9FE24AB196305FD92EC82E647616C3A3694441FB3422E7838E24DEAE\"\nNSString *priKey = keyPair.privateKey;\n```\n\n## SM2 加解密\n\n```objc\n// 明文（字符串类型）\nNSString *plaintext = @\"123456\";\n\n// SM2 加密字符串类型，结果为 ASN1 格式的密文，并编码为 HEX 格式\nNSString *asn1Hex = [GMSm2Utils encryptText:plaintext publicKey:pubKey];\n// 解密得到字符串明文 \"123456\"\nNSString *plaintext = [GMSm2Utils decryptHex:asn1Hex privateKey:priKey];\n\n// ASN1 解码为 C1C3C2 格式（HEX 编码格式）\nNSString *c1c3c2Hex = [GMSm2Utils asn1DecodeToC1C3C2Hex:asn1Hex hasPrefix:NO];\n// 密文顺序 C1C3C2 和 C1C2C3 可相互转换\nNSString *c1c2c3Hex = [GMSm2Utils convertC1C3C2HexToC1C2C3:c1c3c2Hex hasPrefix:NO];\n```\n\n## SM2 签名验签\n\n```objc\nNSString *plaintext = @\"123456\";\n// userID 传入 nil 或空时默认 1234567812345678；不为空时，签名和验签需要相同 ID\nNSString *userID = @\"lifei_zdjl@126.com\";\n// 签名结果是 RS 拼接的 128 字节 Hex 格式字符串，前 64 字节是 R，后 64 字节是 S\nNSString *signRS = [GMSm2Utils signText:plaintext privateKey:priKey userText:userID];\n// 验证签名，返回 YES 验签成功，否则验签失败\nBOOL isOK = [GMSm2Utils verifyText:plaintext signRS:signRS publicKey:pubKey userText:userID];\n```\n\n## ECDH 密钥协商\n\n1. 客户端随机生成一对公私钥 clientPubKey，clientPriKey；\n2. 服务端随机生成一对公私钥 serverPubKey，serverPriKey；\n3. 双方利用网络请求或其他方式交换公钥 clientPubKey 和 serverPubKey，私钥自己保存；\n4. 双方各自计算出的 clientECDH 和 serverECDH 应该是相等的，这个 key 可以作为对称加密的密钥。\n\n```objc\n// 客户端client从服务端server获取公钥serverPubKey，client协商出32字节对称密钥clientECDH，转Hex后为64字节\nNSString *clientECDH = [GMSm2Utils computeECDH:serverPubKey privateKey:clientPriKey];\n// 客户端client将公钥clientPubKey发送给服务端server，server协商出32字节对称密钥serverECDH，转Hex后为64字节\nNSString *serverECDH = [GMSm2Utils computeECDH:clientPubKey privateKey:serverPriKey];\n\n// 在全部明文传输的情况下，client与server协商出相等的对称密钥，clientECDH==serverECDH 成立\nif ([clientECDH isEqualToString:serverECDH]) {\n    NSLog(@\"ECDH 密钥协商成功，协商出的对称密钥为：\\n%@\", clientECDH);\n}else{\n    NSLog(@\"ECDH 密钥协商失败\");\n}\n```\n\n## SM3 摘要\n\nSM3 摘要算法可对文本和文件进行摘要计算，SM3 摘要长度为 64 字节的 HEX 编码格式字符串。\n\n```objc\n// 字符串输入，返回十六进制摘要\nNSString *digest = [GMSm3Utils hashWithText:@\"Hello, SM3!\"];\n\n// 默认使用 SM3 计算 HMAC 摘要，同时支持 MD5、SHA1、SHA224/256/384/512 等其他算法\nNSString *hmac = [GMSm3Utils hmacWithText:@\"Message\" keyText:@\"SecretKey\"];\n```\n\n## SM4 加解密\n\nSM4 对称加密较简单，支持 ECB 和 CBC 两种加密模式。\n\n- ECB 电子密码本模式，密文分割成长度相等的块（不足补齐），逐个块加密。\n- CBC 密文分组链接模式，前一个分组的密文和当前分组的明文异或或操作后再加密。\n\n```objc\n// 字符串加解密，HEX 编码格式密钥长度为 32 字节\nNSString *sm4KeyHex = @\"0123456789abcdef0123456789abcdef\";\nNSString *plaintext = @\"Hello, SM4!\";\n\n// ECB加密。密文为 HEX 编码格式\nNSString *ciphertext = [GMSm4Utils encryptTextWithECB:plaintext keyHex:sm4KeyHex];\n// 解密。解密结果为 \"Hello, SM4!\"\nNSString *decrypted = [GMSm4Utils decryptTextWithECB:ciphertext keyHex:sm4KeyHex];\n\n// CBC 模式需要 16 字节（HEX 编码格式为 32 字节）初始化向量(IV)\nNSString *ivecHex = @\"0123456789abcdef0123456789abcdef\";\n// 加密。密文为 HEX 编码格式\nNSString *ciphertext = [GMSm4Utils encryptTextWithCBC:plaintext keyHex:sm4KeyHex ivecHex:ivecHex];\n// 解密。解密结果为 \"Hello, SM4!\"\nNSString *decrypted = [GMSm4Utils decryptTextWithCBC:ciphertext keyHex:sm4KeyHex ivecHex:ivecHex];\n```\n\n## 版本记录\n\n**警告**：版本 4.0.0 改动较大，与 3.x.x 的 API 名称不兼容，如需升级请留意编译错误。\n\n| GMObjC 版本 |   支持架构   | 兼容平台 |         兼容版本          |\n| :---------: | :----------: | :------: | :-----------------------: |\n|    4.0.3    | x86_64 arm64 | iOS OSX  | iOS\u003e= iOS 9.0, OSX\u003e=10.13 |\n|    3.3.8    | x86_64 arm64 |   iOS    |        \u003e= iOS 9.0         |\n\n## License\n\nGMObjC 在 MIT 许可下发布，详见 [LICENSE](https://github.com/muzipiao/GMObjC/blob/master/LICENSE)。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuzipiao%2Fgmobjc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmuzipiao%2Fgmobjc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuzipiao%2Fgmobjc/lists"}