|
| 1 | +#ifndef CHECK_LICENCE_H |
| 2 | +#define CHECK_LICENCE_H |
| 3 | + |
| 4 | +#if defined(WIN32) || defined(WIN64) |
| 5 | +#pragma warning(disable: 4819) // file codec warning, that's boring! |
| 6 | +#define TLS_API __declspec(dllexport) |
| 7 | +#else |
| 8 | +#define TLS_API |
| 9 | +#endif |
| 10 | + |
| 11 | +#include <stdint.h> |
| 12 | + |
| 13 | +#include <string> |
| 14 | + |
| 15 | +/* |
| 16 | + * tls_gen_signature_ex 接收一系列参数,返回 sig |
| 17 | + * |
| 18 | + * @param dwExpire 过期时长,以秒为单位,建议不超过一个月,如果签名有效期为 10 天,那就填 10*24*3600 |
| 19 | + * @param strAppid3Rd 第三方开放平台账号 appid,如果是自有的账号,那么直接填 sdkappid 的字符串形式 |
| 20 | + * @param dwSdkAppid 创建应用时页面上分配的 sdkappid |
| 21 | + * @param strIdentifier 用户标示符,也就是我们常说的用户 id |
| 22 | + * @param dwAccountType 创建应用时页面上分配的 accounttype |
| 23 | + * @param strSig 返回的 sig |
| 24 | + * @param pPriKey 私钥内容,请注意不是私钥文件名 |
| 25 | + * @param uPriKeyLen 私钥内容长度 |
| 26 | + * @param strErrMsg 如果出错这里出错信息 |
| 27 | + * |
| 28 | + * @return 0 表示成功,非 0 表示失败,失败信息会在 strErrMsg 中给出 |
| 29 | + */ |
| 30 | +TLS_API int tls_gen_signature_ex( |
| 31 | + uint32_t dwExpire, |
| 32 | + const std::string& strAppid3Rd, |
| 33 | + uint32_t dwSdkAppid, |
| 34 | + const std::string& strIdentifier, |
| 35 | + uint32_t dwAccountType, |
| 36 | + std::string& strSig, |
| 37 | + const char* pPriKey, |
| 38 | + uint32_t uPriKeyLen, |
| 39 | + std::string& strErrMsg |
| 40 | +); |
| 41 | + |
| 42 | +/* |
| 43 | + * @brief tls_gen_signature_ex2_with_expire 接收一系列参数,返回 sig |
| 44 | + * |
| 45 | + * @param dwSdkAppid 创建应用时页面上分配的 sdkappid |
| 46 | + * @param strIdentifier 用户标示符,也就是我们常说的用户 id |
| 47 | + * @param dwExpire 开发者自定义的有效期,单位是秒,推荐时长为 1 个月 |
| 48 | + * @param strSig 返回的 sig |
| 49 | + * @param strPriKey 私钥内容,请注意不是私钥文件名 |
| 50 | + * @param strErrMsg 如果出错这里出错信息 |
| 51 | + * |
| 52 | + * @return 0 表示成功,非 0 表示失败,失败信息会在 strErrMsg 中给出 |
| 53 | + */ |
| 54 | +TLS_API int tls_gen_signature_ex2_with_expire( |
| 55 | + uint32_t dwSdkAppid, |
| 56 | + const std::string& strIdentifier, |
| 57 | + uint32_t dwExpire, |
| 58 | + std::string& strSig, |
| 59 | + std::string& strPriKey, |
| 60 | + std::string& strErrMsg); |
| 61 | + |
| 62 | +/* |
| 63 | + * @brief tls_gen_signature_ex2 接收一系列参数,返回 sig,有效采用默认的180天 |
| 64 | + * |
| 65 | + * @param dwSdkAppid 创建应用时页面上分配的 sdkappid |
| 66 | + * @param strIdentifier 用户标示符,也就是我们常说的用户 id |
| 67 | + * @param strSig 返回的 sig |
| 68 | + * @param strPriKey 私钥内容,请注意不是私钥文件名 |
| 69 | + * @param strErrMsg 如果出错这里出错信息 |
| 70 | + * |
| 71 | + * @return 0 表示成功,非 0 表示失败,失败信息会在 strErrMsg 中给出 |
| 72 | + */ |
| 73 | +TLS_API int tls_gen_signature_ex2( |
| 74 | + uint32_t dwSdkAppid, |
| 75 | + const std::string& strIdentifier, |
| 76 | + std::string& strSig, |
| 77 | + std::string& strPriKey, |
| 78 | + std::string& strErrMsg |
| 79 | +); |
| 80 | + |
| 81 | +/** |
| 82 | + * @brief 描述 sig 内容的结构体,各个字段的含义可以参考 tls_gen_signature_ex() |
| 83 | + * @see tls_gen_signature_ex() |
| 84 | + */ |
| 85 | +typedef struct |
| 86 | +{ |
| 87 | + std::string strAccountType; |
| 88 | + std::string strAppid3Rd; |
| 89 | + std::string strAppid; /**< 即 sdkappid */ |
| 90 | + std::string strIdentify; |
| 91 | +} SigInfo; |
| 92 | + |
| 93 | +/** |
| 94 | + * @brief 校验签名,兼容目前所有版本。 |
| 95 | + * @param sig 签名内容 |
| 96 | + * @param key 密钥,如果是早期非对称版本,那么这里是公钥 |
| 97 | + * @param pubKeyLen 密钥内容长度 |
| 98 | + * @param sigInfo 需要校验的签名明文信息 |
| 99 | + * @param expireTime 传出参数,有效期,单位秒 |
| 100 | + * @param initTime 传出参数,签名生成的 unix 时间戳 |
| 101 | + * @param errMsg 传出参数,如果出错,这里有错误信息 |
| 102 | + * @return 0 为成功,非 0 为失败 |
| 103 | + */ |
| 104 | +TLS_API int tls_check_signature_ex( |
| 105 | + const std::string& sig, |
| 106 | + const char* key, |
| 107 | + uint32_t pubKeyLen, |
| 108 | + const SigInfo& sigInfo, |
| 109 | + uint32_t& expireTime, |
| 110 | + uint32_t& initTime, |
| 111 | + std::string& errMsg); |
| 112 | + |
| 113 | +/** |
| 114 | + * @brief 验证 sig 是否合法 |
| 115 | + * |
| 116 | + * @param strSig sig 的内容 |
| 117 | + * @param strPubKey 公钥的内容 |
| 118 | + * @param dwSdkAppid 应用的 sdkappid |
| 119 | + * @param strIdentifier 用户id,会与 sig 中的值进行对比 |
| 120 | + * @param dwExpireTime 返回 sig 的有效期 |
| 121 | + * @param dwInitTime 返回 sig 的生成时间 |
| 122 | + * @param strErrMsg 如果出错,这里有错误信息 |
| 123 | + * |
| 124 | + * @return 0 表示成功,非 0 表示失败,strErrMsg 中有失败信息 |
| 125 | + */ |
| 126 | +TLS_API int tls_check_signature_ex2( |
| 127 | + const std::string& strSig, |
| 128 | + const std::string& strPubKey, |
| 129 | + uint32_t dwSdkAppid, |
| 130 | + const std::string& strIdentifier, |
| 131 | + uint32_t& dwExpireTime, |
| 132 | + uint32_t& dwInitTime, |
| 133 | + std::string& strErrMsg |
| 134 | +); |
| 135 | + |
| 136 | +/** |
| 137 | + * @brief 生成 sig,此函数已“不推荐”使用 |
| 138 | + * @see tls_check_signature_ex() |
| 139 | + * |
| 140 | + * @param strJson 输入参数的 json 串 |
| 141 | + * strJson 示例 |
| 142 | + * { |
| 143 | + * "TLS.account_type": "107", |
| 144 | + * "TLS.appid_at_3rd": "150000000", |
| 145 | + * "TLS.identity": "xxx_openid", |
| 146 | + * "TLS.sdk_appid": "150000000", |
| 147 | + * "TLS.expire_after": "86400" |
| 148 | + * } |
| 149 | + * 值得说明的是 TLS.appid_at_3rd,如果不是第三方开放平台的账号,那么这个字段填写与 TLS.sdk_appid 一致就可以了。 |
| 150 | + * @param strSig 返回 sig 的内容 |
| 151 | + * @param pPriKey 私钥内容,注意不是私钥文件的路径 |
| 152 | + * @param uPriKeyLen 私钥内容的长度 |
| 153 | + * @param strErrMsg 如果出错,这里有出错信息 |
| 154 | + * @param dwFlag 为时间格式,目前默认即可 |
| 155 | + * |
| 156 | + * @return 返回 0 表示成功,非 0 失败,strErrMsg 有出错信息 |
| 157 | + */ |
| 158 | +TLS_API int tls_gen_signature( |
| 159 | + const std::string& strJson, |
| 160 | + std::string& strSig, |
| 161 | + const char* pPriKey, |
| 162 | + uint32_t uPriKeyLen, |
| 163 | + std::string& strErrMsg, |
| 164 | + uint32_t dwFlag = 0 |
| 165 | + ); |
| 166 | + |
| 167 | +enum { |
| 168 | + CHECK_ERR1 = 1, // sig 为空 |
| 169 | + CHECK_ERR2 , // sig base64 解码失败 |
| 170 | + CHECK_ERR3 , // sig zip 解压缩失败 |
| 171 | + CHECK_ERR4 , // sig 使用 json 解析时失败 |
| 172 | + CHECK_ERR5 , // sig 使用 json 解析时失败 |
| 173 | + CHECK_ERR6 , // sig 中 json 串 sig 字段 base64 解码失败 |
| 174 | + CHECK_ERR7 , // sig 中字段缺失 |
| 175 | + CHECK_ERR8 , // sig 校验签名失败,一般是秘钥不正确 |
| 176 | + CHECK_ERR9 , // sig 过期 |
| 177 | + CHECK_ERR10 , // sig 使用 json 解析时失败 |
| 178 | + CHECK_ERR11 , // sig 中 appid_at_3rd 与明文不匹配 |
| 179 | + CHECK_ERR12 , // sig 中 acctype 与明文不匹配 |
| 180 | + CHECK_ERR13 , // sig 中 identifier 与明文不匹配 |
| 181 | + CHECK_ERR14 , // sig 中 sdk_appid 与明文不匹配 |
| 182 | + CHECK_ERR15 , // sig 中 userbuf 异常 |
| 183 | + CHECK_ERR16 , // 内部错误 |
| 184 | + CHECK_ERR17 , // 签名失败 可能是私钥有误 |
| 185 | + |
| 186 | + CHECK_ERR_MAX, |
| 187 | +}; |
| 188 | + |
| 189 | +#define API_VERSION "201803230000" |
| 190 | + |
| 191 | +/* |
| 192 | + * @brief tls_gen_userbuf_ticket |
| 193 | + * |
| 194 | + * @param dwSdkAppid 创建应用时页面上分配的 sdkappid |
| 195 | + * @param strIdentifier 用户标示符,也就是我们常说的用户 id |
| 196 | + * @param dwExpire 开发者自定义的有效期,单位是秒 |
| 197 | + * @param strSig 返回的 sig |
| 198 | + * @param strPriKey 私钥内容,请注意不是私钥文件名 |
| 199 | + * @param strUserbuf 用户自定义内容 |
| 200 | + * @param strErrMsg 如果出错这里出错信息 |
| 201 | + * |
| 202 | + * @return 0 表示成功,非 0 表示失败,失败信息会在 strErrMsg 中给出 |
| 203 | + */ |
| 204 | +TLS_API int tls_gen_userbuf_ticket( |
| 205 | + uint32_t dwSdkAppid, |
| 206 | + const std::string& strIdentifier, |
| 207 | + uint32_t dwExpire, |
| 208 | + const std::string& strPriKey, |
| 209 | + const std::string& strUserbuf, |
| 210 | + std::string& strTicket, |
| 211 | + std::string& strErrMsg); |
| 212 | + |
| 213 | +/** |
| 214 | + * @brief 验证 sig 是否合法 |
| 215 | + * |
| 216 | + * @param strSig sig 的内容 |
| 217 | + * @param strPubKey 公钥的内容 |
| 218 | + * @param dwSdkAppid 应用的 sdkappid |
| 219 | + * @param strIdentifier 用户id,会与 sig 中的值进行对比 |
| 220 | + * @param dwExpireTime 返回 sig 的有效期 |
| 221 | + * @param dwInitTime 返回 sig 的生成时间 |
| 222 | + * @param strUserbuf 返回生成时的userbuf |
| 223 | + * @param strErrMsg 如果出错,这里有错误信息 |
| 224 | + * |
| 225 | + * @return 0 表示成功,非 0 表示失败,strErrMsg 中有失败信息 |
| 226 | + */ |
| 227 | +TLS_API int tls_check_userbuf_ticket( |
| 228 | + const std::string& strTicket, |
| 229 | + const std::string& strPubKey, |
| 230 | + uint32_t dwSdkAppid, |
| 231 | + const std::string& strIdentifier, |
| 232 | + uint32_t& dwExpireTime, |
| 233 | + uint32_t& dwInitTime, |
| 234 | + std::string& strUserbuf, |
| 235 | + std::string& strErrMsg |
| 236 | +); |
| 237 | + |
| 238 | +TLS_API int gen_sig(uint32_t sdkappid, const std::string& identifier, const std::string& priKey, std::string& sig); |
| 239 | + |
| 240 | +/** |
| 241 | + * @brief 生成签名函数 v2 版本 |
| 242 | + * @param sdkappid 应用ID |
| 243 | + * @param identifier 用户账号,utf-8 编码 |
| 244 | + * @param key 密钥 |
| 245 | + * @param expire 有效期,单位秒 |
| 246 | + * @param errMsg 错误信息 |
| 247 | + * @return 0 为成功,非 0 为失败 |
| 248 | + */ |
| 249 | +TLS_API int gen_sig_v2(uint32_t sdkappid, const std::string& identifier, |
| 250 | + const std::string& key, int expire, std::string& sig, std::string& errMsg); |
| 251 | + |
| 252 | +int thread_setup(); |
| 253 | +void thread_cleanup(); |
| 254 | + |
| 255 | +namespace tls_signature_inner { |
| 256 | +TLS_API int SigToJson(const std::string &sig, std::string &json, std::string &errmsg); |
| 257 | +} |
| 258 | + |
| 259 | +#endif |
| 260 | + |
0 commit comments