使用 Go 實現 License 認證
在軟件授權管理中,License 機制可以有效防止未授權使用,確保軟件的合法性。本篇文章將講解如何使用 Go 生成機器碼、創建 License、驗證 License 及防止時間篡改,並提供完整可運行代碼示例,以讓您理解其中的邏輯。
License 機制概述
在軟件授權體系中,License(許可證)是用於驗證軟件的合法使用權限的一種機制。常見 License 設計包含以下關鍵要素:
-
機器碼:用於唯一標識設備,防止 License 盜用。
-
到期時間:控制 License 的有效期,到期後軟件不可用。
-
簽名校驗:使用 HMAC-SHA256 或者其他進行 License 簽名,防止篡改。
-
防止篡改系統時間:存儲上次運行時間,防止用戶回退系統時間繞過 License 過期檢測。
實現思路
我們將實現如下功能:
1. 生成機器碼:基於 MAC 地址 + CPU ID 計算 SHA256 哈希,保證唯一性。
2. 生成 License:包含機器碼、過期時間、數字簽名,防止篡改。
3. 存儲 License:將 License 存入本地文件,在系統啓動時驗證。
4. 校驗 License:
-
解析 License 並驗證簽名和過期時間。
-
若 License 無效或過期,軟件拒絕運行。
5. 防止篡改時間繞過 License 過期:
- 本地存儲上次運行時間,若時間回退則拒絕運行。
代碼實現
生成機器碼
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net"
"os/exec"
"runtime"
"strings"
)
// 獲取 MAC 地址
func getMacAddress() string {
interfaces, err := net.Interfaces()
if err != nil {
return "unknown"
}
for _, iface := range interfaces {
if len(iface.HardwareAddr) > 0 {
return iface.HardwareAddr.String()
}
}
return "unknown"
}
// 獲取 CPU ID
func getCpuID() string {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("wmic", "cpu", "get", "ProcessorId")
case "linux":
cmd = exec.Command("cat", "/proc/cpuinfo")
case "darwin":
cmd = exec.Command("sysctl", "-n", "machdep.cpu.brand_string")
default:
return "unknown"
}
output, err := cmd.Output()
if err != nil {
return "unknown"
}
return strings.TrimSpace(string(output))
}
// 生成機器碼
func generateMachineCode() string {
data := getMacAddress() + getCpuID()
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])
}
License 結構
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"time"
)
// License 結構
type License struct {
MachineCode string `json:"machine_code"`
ExpireAt int64 `json:"expire_at"`
Signature string `json:"signature"`
}
var secretKey = "my-secret-key" // License 簽名密鑰
// 生成 License 簽名
func generateSignature(machineCode string, expireAt int64) string {
data := fmt.Sprintf("%s:%d", machineCode, expireAt)
h := hmac.New(sha256.New, []byte(secretKey))
h.Write([]byte(data))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// 生成 License
func generateLicense(machineCode string, days int) string {
expireAt := time.Now().Add(time.Duration(days) * 24 * time.Hour).Unix()
signature := generateSignature(machineCode, expireAt)
license := License{
MachineCode: machineCode,
ExpireAt: expireAt,
Signature: signature,
}
licenseBytes, _ := json.Marshal(license)
return base64.StdEncoding.EncodeToString(licenseBytes)
}
存儲與讀取 License
// 存儲 License 到本地
func saveLicenseToFile(license string) error {
return ioutil.WriteFile("license.txt", []byte(license), 0644)
}
// 讀取 License
func loadLicenseFromFile() (string, error) {
data, err := ioutil.ReadFile("license.txt")
if err != nil {
return "", err
}
return string(data), nil
}
解析與驗證 License
// 解析 License
func parseLicense(encodedLicense string) (*License, error) {
licenseBytes, err := base64.StdEncoding.DecodeString(encodedLicense)
if err != nil {
return nil, err
}
var license License
if err := json.Unmarshal(licenseBytes, &license); err != nil {
return nil, err
}
// 驗證簽名
expectedSignature := generateSignature(license.MachineCode, license.ExpireAt)
if license.Signature != expectedSignature {
return nil, errors.New("invalid signature")
}
// 檢查是否過期
if time.Now().Unix() > license.ExpireAt {
return nil, errors.New("license expired")
}
return &license, nil
}
防止用戶回退系統時間
// 記錄上次運行時間
type LastRun struct {
Timestamp int64 `json:"timestamp"`
}
// 保存上次運行時間
func saveLastRunTime() error {
data, _ := json.Marshal(LastRun{Timestamp: time.Now().Unix()})
return ioutil.WriteFile("last_run.json", data, 0644)
}
// 讀取上次運行時間
func loadLastRunTime() (int64, error) {
data, err := ioutil.ReadFile("last_run.json")
if err != nil {
return 0, err
}
var lastRun LastRun
json.Unmarshal(data, &lastRun)
return lastRun.Timestamp, nil
}
// 檢測系統時間是否被回退
func checkTimeValidity() bool {
lastRun, err := loadLastRunTime()
if err == nil && time.Now().Unix() < lastRun {
return false // 系統時間被回退,拒絕啓動
}
saveLastRunTime()
return true
}
啓動時驗證 License
func main() {
machineCode := generateMachineCode()
fmt.Println("Machine Code:", machineCode)
// 讀取 License
licenseStr, err := loadLicenseFromFile()
if err != nil {
fmt.Println("No valid license found. Generating a new one...")
licenseStr = generateLicense(machineCode, 30) // 生成 30 天 License
saveLicenseToFile(licenseStr)
}
// 驗證 License
license, err := parseLicense(licenseStr)
if err != nil {
fmt.Println("License invalid:", err)
fmt.Println("System exiting...")
os.Exit(1)
}
fmt.Println(license)
// 檢測是否篡改時間
if !checkTimeValidity() {
fmt.Println("System time tampered. Exiting...")
os.Exit(1)
}
fmt.Println("License is valid. System running...")
}
運行結果如下所示:
Machine Code: 716372d3f7cd9d0c3cce2b9f08d3b8d133f98c2be8747cbc94ee76f9652a8fsa
No valid license found. Generating a new one...
&{716372d3f7cd9d0c3cce2b9f08d3b8d133f98c2be8747cbc94ee76f9652a8fsa 1260486000 McRH96QqLC0sXKnAS9v5Aw/RLnfVU2PAHq37jVLut4w=}
License is valid. System running...
總結
以上就是今天的所有內容,我們模擬式的實現瞭如下的功能:
-
機器碼綁定設備
-
License 簽名防篡改
-
License 過期檢測
-
防止用戶回退時間繞過授權
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/5s47DYUtottu7f8Jf792-Q