使用 Go 實(shí)現(xiàn) License 認(rèn)證
在軟件授權(quán)管理中,License機(jī)制可以有效防止未授權(quán)使用,確保軟件的合法性。本篇文章將講解如何使用Go生成機(jī)器碼、創(chuàng)建License、驗(yàn)證License及防止時(shí)間篡改,并提供完整可運(yùn)行代碼示例,以讓您理解其中的邏輯。
License機(jī)制概述
在軟件授權(quán)體系中,License(許可證)是用于驗(yàn)證軟件的合法使用權(quán)限的一種機(jī)制。常見License設(shè)計(jì)包含以下關(guān)鍵要素:
- 機(jī)器碼:用于唯一標(biāo)識(shí)設(shè)備,防止License盜用。
- 到期時(shí)間:控制License的有效期,到期后軟件不可用。
- 簽名校驗(yàn):使用HMAC-SHA256或者其他進(jìn)行License簽名,防止篡改。
- 防止篡改系統(tǒng)時(shí)間:存儲(chǔ)上次運(yùn)行時(shí)間,防止用戶回退系統(tǒng)時(shí)間繞過License過期檢測(cè)。
實(shí)現(xiàn)思路
我們將實(shí)現(xiàn)如下功能:
(1) 生成機(jī)器碼:基于MAC地址 + CPU ID計(jì)算SHA256哈希,保證唯一性。
(2) 生成License:包含機(jī)器碼、過期時(shí)間、數(shù)字簽名,防止篡改。
(3) 存儲(chǔ)License:將License存入本地文件,在系統(tǒng)啟動(dòng)時(shí)驗(yàn)證。
(4) 校驗(yàn)License:
- 解析License并驗(yàn)證簽名和過期時(shí)間。
- 若License無效或過期,軟件拒絕運(yùn)行。
(5) 防止篡改時(shí)間繞過License過期:本地存儲(chǔ)上次運(yùn)行時(shí)間,若時(shí)間回退則拒絕運(yùn)行。
代碼實(shí)現(xiàn)
生成機(jī)器碼:
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))
}
// 生成機(jī)器碼
func generateMachineCode() string {
data := getMacAddress() + getCpuID()
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])
}
License結(jié)構(gòu):
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"time"
)
// License 結(jié)構(gòu)
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)
}
存儲(chǔ)與讀取License:
// 存儲(chǔ) 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
}
解析與驗(yàn)證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
}
// 驗(yàn)證簽名
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
}
防止用戶回退系統(tǒng)時(shí)間:
// 記錄上次運(yùn)行時(shí)間
type LastRun struct {
Timestamp int64 `json:"timestamp"`
}
// 保存上次運(yùn)行時(shí)間
func saveLastRunTime() error {
data, _ := json.Marshal(LastRun{Timestamp: time.Now().Unix()})
return ioutil.WriteFile("last_run.json", data, 0644)
}
// 讀取上次運(yùn)行時(shí)間
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
}
// 檢測(cè)系統(tǒng)時(shí)間是否被回退
func checkTimeValidity() bool {
lastRun, err := loadLastRunTime()
if err == nil && time.Now().Unix() < lastRun {
return false // 系統(tǒng)時(shí)間被回退,拒絕啟動(dòng)
}
saveLastRunTime()
return true
}
啟動(dòng)時(shí)驗(yàn)證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)
}
// 驗(yàn)證 License
license, err := parseLicense(licenseStr)
if err != nil {
fmt.Println("License invalid:", err)
fmt.Println("System exiting...")
os.Exit(1)
}
fmt.Println(license)
// 檢測(cè)是否篡改時(shí)間
if !checkTimeValidity() {
fmt.Println("System time tampered. Exiting...")
os.Exit(1)
}
fmt.Println("License is valid. System running...")
}
運(yùn)行結(jié)果如下所示:
Machine Code: 716372d3f7cd9d0c3cce2b9f08d3b8d133f98c2be8747cbc94ee76f9652a8fsa
No valid license found. Generating a new one...
&{716372d3f7cd9d0c3cce2b9f08d3b8d133f98c2be8747cbc94ee76f9652a8fsa 1260486000 McRH96QqLC0sXKnAS9v5Aw/RLnfVU2PAHq37jVLut4w=}
License is valid. System running...
總結(jié)
以上就是今天的所有內(nèi)容,我們模擬式的實(shí)現(xiàn)了如下的功能:
- 機(jī)器碼綁定設(shè)備
- License簽名防篡改
- License過期檢測(cè)
- 防止用戶回退時(shí)間繞過授權(quán)
雖然是以模擬的方式實(shí)現(xiàn)的,但還是希望能夠在你實(shí)戰(zhàn)時(shí)提供一點(diǎn)思路和幫助。