# license_robot **Repository Path**: huangjinwangpu/license_robot ## Basic Information - **Project Name**: license_robot - **Description**: license_robot用来对软件模块做授权.license文件加密,根据硬件生成唯一序列号 - **Primary Language**: C++ - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 10 - **Created**: 2020-03-17 - **Last Updated**: 2026-02-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # license_robot license_robot 是软件授权库,支持: - 模块化授权 - 层次化授权:采用路径记录各层级,子路径继承父路径授权 - 机器绑定:license 文件名含机器序列号(根据硬件生成),防止滥用 - 防篡改:license 内容 SHA1/SHA256 摘要校验 --- ## 项目结构 ``` license_robot/ ├── CMakeLists.txt ├── Doxyfile # Doxygen 配置,用于生成 API 文档 ├── README.md ├── LICENSE ├── source/ # 源码 │ ├── license.c/h # 核心逻辑 │ ├── licgen.c/h # 机器序列号 │ ├── sha1.c/h # SHA1 实现 │ ├── sha256.c/h # SHA256 实现 │ ├── license_gen.c # License 生成器主程序 │ └── liclock.h # C++ 封装(依赖主项目 grutil.hpp) ├── tests/ # 测试与示例 │ ├── CMakeLists.txt │ ├── test_verify.c # 单元测试 │ └── example_usage.c # 动态库调用示例(完整 API 演示) ├── scripts/ # 常用脚本 │ ├── build.sh # 编译 │ ├── clean.sh # 清理 │ ├── test.sh # 运行测试 │ ├── gen_license.sh # 生成 license │ ├── verify_license.sh # 校验 license │ ├── get_sn.sh # SN 说明 │ └── arch.sh # 检测架构 (x86/x64) └── build/ ├── lib/libgrlic.so # 动态库 ├── lib/libgrlic.a # 静态库 ├── bin/grlicgen # 可执行程序(生成 license) └── install/ # make install 默认输出目录 ├── lib/ ├── include/grlic/ └── bin/ ``` --- ## 编译 ```bash mkdir build && cd build cmake .. make ``` 或使用脚本:`./scripts/build.sh` 产物: - `build/lib/libgrlic.so` — 动态库,供其他程序链接以加载、校验 license - `build/lib/libgrlic.a` — 静态库,供静态链接 - `build/bin/grlicgen` — 可执行程序,用于生成 license 文件 ### 安装 ```bash cd build && make install ``` 默认安装到 `build/install/`(可通过 `cmake -DCMAKE_INSTALL_PREFIX=/path ..` 自定义): - `install/lib/libgrlic.so`、`install/lib/libgrlic.a` — 动态库与静态库 - `install/include/grlic/license.h` — 公共头文件(集成时只需此头文件) - `install/bin/grlicgen` — 生成器 ### 生成 API 文档 ```bash doxygen Doxyfile ``` 生成的 HTML 文档位于 `doc/html/index.html`。 --- ## 使用 ### 1. 生成 License(grlicgen) ```bash # 为当前机器生成 license ./build/bin/grlicgen -o grgrant.xxx.lic -s current -m 5 -f 1 -v 100 # 为指定机器 SN 生成(需目标机器的 16 位十六进制 SN) ./build/bin/grlicgen -o grgrant.xxx.lic -s A1B2C3D4E5F67890 -m 5 -f 1 -v 100 ``` 参数说明: | 参数 | 说明 | |------|------| | -o | 输出 license 文件路径 | | -s | 机器 SN(16 位十六进制)或 `current`(当前机器) | | -m | 模块 ID(如 5=glm_general5) | | -f | Facet ID(如 1=glfq_max_cam) | | -v | 整型配额值 | | -e | 生效时间(Unix 时间戳,默认当前) | | -x | 过期时间(0=永不过期) | | -V | 格式版本:1=SHA1(默认),2=SHA256 | ### 2. 在程序中校验 License(动态库) ```bash # 方式一:使用 build 产物 gcc -o myapp myapp.c -Isource -Lbuild/lib -lgrlic -Wl,-rpath,'$ORIGIN/../lib' # 方式二:使用 install 产物 gcc -o myapp myapp.c -Ibuild/install/include/grlic -Lbuild/install/lib -lgrlic -Wl,-rpath,'$ORIGIN/../lib' ``` ```c #include "license.h" gr_license_t lic; gr_credit_path_t path = { .module = glm_general5, .facet = glfq_max_cam }; int val; // 指定文件加载 if (grlic_load("grgrant.xxx.lic", &lic) != 0) return -1; // 或自动查找当前目录匹配的 license if (grlic_take(&lic) != 0) return -1; if (grlic_get_int(&lic, &path, &val) == 0) printf("max_cam = %d\n", val); grlic_free(&lic); ``` 错误码:`GRLIC_OK`(0)、`GRLIC_ERR_OPEN`(-2)、`GRLIC_ERR_SN`(-3)、`GRLIC_ERR_DIGEST`(-4)、`GRLIC_ERR_EXPIRED`(-5)、`GRLIC_ERR_NOT_EFFECTIVE`(-6)。 `grlic_get_str` 需传入 `buf_size` 参数,确保缓冲区安全。 **可运行示例**:`./build/bin/example_usage [license文件]` 演示完整调用流程(加载、校验、读取、获取 SN)。 ### 3. 脚本快捷方式 ```bash ./scripts/build.sh # 编译 ./scripts/test.sh # 运行测试 ./scripts/gen_license.sh out.lic 100 # 生成 license(SHA256) ./scripts/verify_license.sh out.lic # 校验 license ./scripts/clean.sh # 清理 ``` --- ## 集成到其他项目 **方式一:add_subdirectory(源码集成)** ```cmake add_subdirectory(third_party/license_robot) target_link_libraries(your_app grlic) target_include_directories(your_app PRIVATE third_party/license_robot/source) ``` **方式二:使用 install 产物(仅需 license.h + libgrlic)** ```cmake set(GRLIC_ROOT /path/to/install) # 指向 make install 输出目录 target_link_directories(your_app PRIVATE ${GRLIC_ROOT}/lib) target_link_libraries(your_app grlic) target_include_directories(your_app PRIVATE ${GRLIC_ROOT}/include/grlic) ``` Windows 需额外链接 `iphlpapi`。 --- ## 平台支持 | 平台 | 支持 | |------|------| | Linux | ✓ | | Windows | ✓ | | macOS | 未测试 | --- ## 许可证 MIT License,详见 [LICENSE](LICENSE)。sha1.c、sha256.c 为 Public Domain。 ## 备注 - **liclock.h**:C++ 封装层,位于 `source/`,仅供主项目集成时使用,依赖 `grutil.hpp`、`grutilex.hpp`、`sysinc.hpp` - **SHA1/SHA256**:v1 使用 SHA1(20 字节),v2 使用 SHA256(32 字节)。`grlicgen -V 2` 生成 v2 格式