' 易语言示例(精简版,完整文件见下方下载)
.版本 2
.程序集 授权模块
.子程序 调用接口, 文本型
.参数 动作, 文本型
.参数 授权码, 文本型
.局部变量 时间戳, 文本型
.局部变量 机器码, 文本型
.局部变量 签名, 文本型
.局部变量 提交数据, 文本型
时间戳 = 到文本 (取现行时间戳 ())
机器码 = 取机器码 ()
签名 = HMAC_SHA256 (动作 + “|” + 产品代码 + “|” + 授权码 + “|” + 机器码 + “|” + 时间戳, API密钥)
提交数据 = “{” + #引号 + “action” + #引号 + “:” + #引号 + 动作 + #引号 + “,”
+ #引号 + “product” + #引号 + “:” + #引号 + 产品代码 + #引号 + “,”
+ #引号 + “code” + #引号 + “:” + #引号 + 授权码 + #引号 + “,”
+ #引号 + “machine” + #引号 + “:” + #引号 + 机器码 + #引号 + “,”
+ #引号 + “ts” + #引号 + “:” + #引号 + 时间戳 + #引号 + “,”
+ #引号 + “sign” + #引号 + “:” + #引号 + 签名 + #引号 + “}”
返回 (网页_访问_对象 (API地址, 1, 提交数据, , , “Content-Type: application/json”, , , , , , , , , , ))
' 响应 JSON 中 ok 为真即成功;activate / verify / heartbeat / unbind 均走此接口
下载完整示例
// JavaScript (Node.js >= 18) 示例
const crypto = require('crypto');
const os = require('os');
const API_URL = 'https://pay.hbylsjcm.cn/api/verify';
const API_KEY = 'your_api_key_here';
const PRODUCT = 'your_product_code';
function getMachineCode() {
for (const iface of Object.values(os.networkInterfaces())) {
for (const cfg of iface) {
if (!cfg.internal && cfg.mac && cfg.mac !== '00:00:00:00:00:00') {
return crypto.createHash('sha256').update(cfg.mac).digest('hex');
}
}
}
return crypto.createHash('sha256').update(os.hostname()).digest('hex');
}
async function apiCall(action, code) {
const machine = getMachineCode();
const ts = Math.floor(Date.now() / 1000).toString();
const sign = crypto.createHmac('sha256', API_KEY)
.update(`${action}|${PRODUCT}|${code}|${machine}|${ts}`)
.digest('hex');
const res = await fetch(API_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action, product: PRODUCT, code, machine, ts, sign }),
});
return res.json(); // ok === true 即成功
}
const activate = (code) => apiCall('activate', code);
const verify = (code) => apiCall('verify', code);
const heartbeat = (code) => apiCall('heartbeat', code);
const unbind = (code) => apiCall('unbind', code);
下载完整示例
// C++ (OpenSSL + libcurl) 示例
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <string>
std::string hmac_sha256(const std::string& key, const std::string& msg) {
unsigned char result[EVP_MAX_MD_SIZE];
unsigned int len = 0;
HMAC(EVP_sha256(), key.c_str(), key.size(),
(const unsigned char*)msg.c_str(), msg.size(), result, &len);
std::string hex;
char buf[3];
for (unsigned int i = 0; i < len; i++) {
snprintf(buf, sizeof(buf), "%02x", result[i]);
hex += buf;
}
return hex;
}
// 签名原文:"{action}|{product}|{code}|{machine}|{ts}"
std::string buildJson(const std::string& action, const std::string& code,
const std::string& machine, const std::string& ts,
const std::string& sign) {
return R"({"action":")" + action + R"(","product":")" + PRODUCT_CODE
+ R"(","code":")" + code + R"(","machine":")" + machine
+ R"(","ts":")" + ts + R"(","sign":")" + sign + R"("})";
}
// 使用 libcurl POST 至 /api/verify,解析响应 JSON 的 ok 字段
下载完整示例
// C# (.NET 6+) 示例
using System;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class LicenseClient
{
private const string ApiUrl = "https://pay.hbylsjcm.cn/api/verify";
private const string ApiKey = "your_api_key_here";
private const string Product = "your_product_code";
private static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(15) };
private static string HmacSha256(string key, string message)
{
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
return Convert.ToHexString(hmac.ComputeHash(Encoding.UTF8.GetBytes(message))).ToLowerInvariant();
}
public async Task<JsonElement> CallApi(string action, string code, string machine)
{
var ts = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
var sign = HmacSha256(ApiKey, $"{action}|{Product}|{code}|{machine}|{ts}");
var json = JsonSerializer.Serialize(new { action, product = Product, code, machine, ts, sign });
using var content = new StringContent(json, Encoding.UTF8, "application/json");
using var response = await Http.PostAsync(ApiUrl, content);
return JsonSerializer.Deserialize<JsonElement>(await response.Content.ReadAsStringAsync());
}
// 响应中 ok 为 true 即成功
public Task<JsonElement> Activate(string code) => CallApi("activate", code, GetMachineCode());
public Task<JsonElement> Verify(string code) => CallApi("verify", code, GetMachineCode());
public Task<JsonElement> Heartbeat(string code) => CallApi("heartbeat", code, GetMachineCode());
public Task<JsonElement> Unbind(string code) => CallApi("unbind", code, GetMachineCode());
}
下载完整示例