lk

package module
v0.0.0-...-0701e81 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 10 Imported by: 0

README

license-key (国密 SM2/SM3 版本)

A simple licensing library in Golang, that generates license files containing arbitrary data (ex: user email, end date...) that you can further validate if you want.

本项目基于(https://github.com/hyperboloide/lk/)已改造为使用国密算法:

  • 签名算法: SM2 (基于 sm2p256v1 曲线,符合 GM/T 0003-2012 标准)
  • 哈希算法: SM3 (符合 GM/T 0004-2012 标准)
  • 依赖库: github.com/emmansun/gmsm

The license file can be marshalled in an easy to distribute format (ex: base32 encoded strings)

Note that this implementation is quite basic and that in no way it could prevent someone to hack your software. The goal of this project is only to provide a convenient way for software publishers to generate license keys and distribute them without too much hassle for the user.

与原版的区别
原版 (ECDSA) 本版 (SM2)
使用 NIST P-384 曲线 使用国密 sm2p256v1 曲线
使用 SHA-256 哈希 使用 SM3 哈希
依赖 crypto/ecdsa 依赖 github.com/emmansun/gmsm/sm2
依赖 crypto/sha256 依赖 github.com/emmansun/gmsm/sm3
How does it works?
  1. Generate a private key (and keep it secure).
  2. Transform the data you want to provide (end date, user email...) to a byte array (using json or gob for example).
  3. The library takes the data and create a cryptographically signed hash using SM2/SM3 that is appended to the data.
  4. Convert the result to a Base64/Base32/Hex string and send it to the end user: this is the license.
  5. When the user starts your program load the license and verify the signature using a public key.
  6. Validate the data in your license key (ex: the end date)
lkgen

A command line helper lkgen is also provided to generate private keys and create licenses.

This is also a good example of how to use the library.

Examples
Generating a new license:

Below is an example of code that generates a license from a private key and a struct containing the end date and a user email that is marshalled to json.

// Generate a new SM2 private key
// In production, you should save this key and load it from a file
privateKey, err := lk.NewPrivateKey()
if err != nil {
	log.Fatal(err)
}

// Save the private key (base32 encoded) for future use
privateKeyB32, err := privateKey.ToB32String()
if err != nil {
	log.Fatal(err)
}
fmt.Println("Private Key:", privateKeyB32)

// Define the data you need in your license,
// here we use a struct that is marshalled to json, but ultimately all you need is a []byte.
doc := struct {
	Email string    `json:"email"`
	End   time.Time `json:"end"`
}{
	"[email protected]",
	time.Now().Add(time.Hour * 24 * 365), // 1 year
}

// marshall the document to []bytes (this is the data that our license will contain).
docBytes, err := json.Marshal(doc)
if err != nil {
	log.Fatal(err)
}

// generate your license with the private key and the document
license, err := lk.NewLicense(privateKey, docBytes)
if err != nil {
	log.Fatal(err)
}

// the b32 representation of our license, this is what you give to your customer.
licenseB32, err := license.ToB32String()
if err != nil {
	log.Fatal(err)
}
fmt.Println("License:", licenseB32)
Validating a license:

Before your execute your program you want to check the user license:

// The public key should be hardcoded in your app (generated from private key)
// You can get it using: privateKey.GetPublicKey().ToB32String()
const publicKeyBase32 = "YOUR_PUBLIC_KEY_HERE"

// A previously generated license b32 encoded (provided by the customer)
const licenseB32 = "CUSTOMER_LICENSE_HERE"

// Unmarshal the public key.
publicKey, err := lk.PublicKeyFromB32String(publicKeyBase32)
if err != nil {
	log.Fatal(err)
}

// Unmarshal the customer license.
license, err := lk.LicenseFromB32String(licenseB32)
if err != nil {
	log.Fatal(err)
}

// validate the license signature using SM2.
if ok, err := license.Verify(publicKey); err != nil {
	log.Fatal(err)
} else if !ok {
	log.Fatal("Invalid license signature")
}

result := struct {
	Email string    `json:"email"`
	End   time.Time `json:"end"`
}{}

// unmarshal the document.
if err := json.Unmarshal(license.Data, &result); err != nil {
	log.Fatal(err)
}

// Now you just have to check that the end date is after time.Now() then you can continue!
if result.End.Before(time.Now()) {
	log.Fatalf("License expired on: %s", result.End.Format("2006-01-02"))
} else {
	fmt.Printf(`Licensed to %s until %s`, result.Email, result.End.Format("2006-01-02"))
}
A Complete example

Bellow is a sample function that generate a key pair, signs a license and verify it.

// create a new SM2 Private key:
privateKey, err := lk.NewPrivateKey()
if err != nil {
	log.Fatal(err)
}

// create a license document:
doc := MyLicence{
	"[email protected]",
	time.Now().Add(time.Hour * 24 * 365), // 1 year
}

// marshall the document to json bytes:
docBytes, err := json.Marshal(doc)
if err != nil {
	log.Fatal(err)
}

// generate your license with the private key and the document (signed using SM2/SM3):
license, err := lk.NewLicense(privateKey, docBytes)
if err != nil {
	log.Fatal(err)
}

// encode the new license to b64, this is what you give to your customer.
str64, err := license.ToB64String()
if err != nil {
	log.Fatal(err)
}
fmt.Println(str64)

// get the public key. The public key should be hardcoded in your app to check licences.
// Do not distribute the private key!
publicKey := privateKey.GetPublicKey()

// validate the license using SM2:
if ok, err := license.Verify(publicKey); err != nil {
	log.Fatal(err)
} else if !ok {
	log.Fatal("Invalid license signature")
}

// unmarshal the document and check the end date:
res := MyLicence{}
if err := json.Unmarshal(license.Data, &res); err != nil {
	log.Fatal(err)
} else if res.End.Before(time.Now()) {
	log.Fatalf("License expired on: %s", res.End.String())
} else {
	fmt.Printf(`Licensed to %s until %s \n`, res.Email, res.End.Format("2006-01-02"))
}
国密算法说明

本项目使用的国密算法:

  • SM2: 椭圆曲线公钥密码算法,用于数字签名。基于 sm2p256v1 曲线,安全性等同于 NIST P-256。
  • SM3: 密码杂凑算法,输出 256 位哈希值,安全性等同于 SHA-256。

更多关于国密算法的信息,请参考:

Documentation

Overview

Example (Complete)

Example_complete creates a new license and validate it.

// create a new Private key:
privateKey, err := lk.NewPrivateKey()
if err != nil {
	log.Fatal("private key generation failed: " + err.Error())

}

// create a license document:
doc := MyLicence{
	"[email protected]",
	time.Now().Add(time.Hour * 24 * 365), // 1 year
}

// marshall the document to json bytes:
docBytes, err := json.Marshal(doc)
if err != nil {
	log.Fatal(err)

}

// generate your license with the private key and the document:
license, err := lk.NewLicense(privateKey, docBytes)
if err != nil {
	log.Fatal("license generation failed: " + err.Error())

}

// encode the new license to b64, this is what you give to your customer.
str64, err := license.ToB64String()
if err != nil {
	log.Fatal(err)
}
fmt.Println(str64)

// get the public key. The public key should be hardcoded in your app
// to check licences. Do not distribute the private key!
publicKey := privateKey.GetPublicKey()

// validate the license:
if ok, err := license.Verify(publicKey); err != nil {
	log.Fatal("license verification failed: " + err.Error())
} else if !ok {
	log.Fatal("Invalid license signature")
}

// unmarshal the document and check the end date:
res := MyLicence{}
if err := json.Unmarshal(license.Data, &res); err != nil {
	log.Fatal(err)
} else if res.End.Before(time.Now()) {
	log.Fatalf("License expired on: %s", res.End.String())
} else {
	fmt.Printf(`Licensed to %s until %s \n`, res.Email, res.End.Format("2006-01-02"))
}
Example (LicenseGeneration)

Example_licenseGeneration shows how to create a license file from a private key using SM2 algorithm.

// Generate a new SM2 private key dynamically for this example
privateKey, err := lk.NewPrivateKey()
if err != nil {
	log.Fatal("private key generation failed: " + err.Error())
}

// Here we use a struct that is marshalled to json,
// but ultimatly all you need is a []byte.
doc := struct {
	Email string    `json:"email"`
	End   time.Time `json:"end"`
}{
	"[email protected]",
	time.Now().Add(time.Hour * 24 * 365), // 1 year
}

// marshall the document to []bytes (this is the data that our license
// will contain):
docBytes, err := json.Marshal(doc)
if err != nil {
	log.Fatal(err)
}

// generate your license with the private key and the document:
license, err := lk.NewLicense(privateKey, docBytes)
if err != nil {
	log.Fatal("license generation failed: " + err.Error())
}

// the b32 representation of our license, this is what you give to
// your customer.
licenseB32, err := license.ToB32String()
if err != nil {
	log.Fatal("license encoding failed: " + err.Error())

}

// print the license that you should give to your customer
fmt.Println(licenseB32)
Example (LicenseVerification)

Example_licenseVerification validates a previously generated license with a public key using SM2 algorithm.

// Generate a new SM2 private key for this example
privateKey, err := lk.NewPrivateKey()
if err != nil {
	log.Fatal("private key generation failed: " + err.Error())
}

// Create license data
doc := struct {
	Email string    `json:"email"`
	End   time.Time `json:"end"`
}{
	"[email protected]",
	time.Now().Add(time.Hour * 24 * 365),
}

docBytes, err := json.Marshal(doc)
if err != nil {
	log.Fatal(err)
}

// Generate license
license, err := lk.NewLicense(privateKey, docBytes)
if err != nil {
	log.Fatal("license generation failed: " + err.Error())
}

// Get the license as B32 string (simulating what a customer would receive)
licenseB32, err := license.ToB32String()
if err != nil {
	log.Fatal("license encoding failed: " + err.Error())
}

// Get the public key (this should be hardcoded in your app)
publicKey := privateKey.GetPublicKey()
publicKeyB32 := publicKey.ToB32String()

// Now simulate the verification process that would happen in your app

// Unmarshal the public key from B32
parsedPublicKey, err := lk.PublicKeyFromB32String(publicKeyB32)
if err != nil {
	log.Fatal("public key unmarshal failed: " + err.Error())
}

// Unmarshal the customer license from B32
parsedLicense, err := lk.LicenseFromB32String(licenseB32)
if err != nil {
	log.Fatal("license unmarshal failed: " + err.Error())
}

// validate the license signature:
if ok, err := parsedLicense.Verify(parsedPublicKey); err != nil {
	log.Fatal("license verification failed: " + err.Error())
} else if !ok {
	log.Fatal("Invalid license signature")
}

result := struct {
	Email string    `json:"email"`
	End   time.Time `json:"end"`
}{}

// unmarshal the document:
if err := json.Unmarshal(parsedLicense.Data, &result); err != nil {
	log.Fatal(err)
}

// Now you just have to check the end date and if it before time.Now(),
// then you can continue!
// if result.End.Before(time.Now()) {
// 	log.Fatal("License expired on: %s", result.End.Format("2006-01-02"))
// } else {
// 	fmt.Printf(`Licensed to %s until %s`, result.Email, result.End.Format("2006-01-02"))
// }

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidPublicKey = errors.New("lk: invalid public key")

ErrInvalidPublicKey is returned when the public key is invalid.

Functions

This section is empty.

Types

type License

type License struct {
	Data []byte
	R    *big.Int
	S    *big.Int
}

License represents a license with some data and a signature.

func LicenseFromB32String

func LicenseFromB32String(str string) (*License, error)

LicenseFromB32String returns a License from a base64 encoded string.

func LicenseFromB64String

func LicenseFromB64String(str string) (*License, error)

LicenseFromB64String returns a License from a base64 encoded string.

func LicenseFromBytes

func LicenseFromBytes(b []byte) (*License, error)

LicenseFromBytes returns a License from a []byte.

func LicenseFromHexString

func LicenseFromHexString(str string) (*License, error)

LicenseFromHexString returns a License from a hexadecimal encoded string.

func NewLicense

func NewLicense(k *PrivateKey, data []byte) (*License, error)

NewLicense create a new license and sign it using SM2.

func (*License) ToB32String

func (l *License) ToB32String() (string, error)

ToB32String transforms the license to a base32 []byte.

func (*License) ToB64String

func (l *License) ToB64String() (string, error)

ToB64String transforms the licence to a base64 []byte.

func (*License) ToBytes

func (l *License) ToBytes() ([]byte, error)

ToBytes transforms the licence to a base64 []byte.

func (*License) ToHexString

func (l *License) ToHexString() (string, error)

ToHexString transforms the license to a hexadecimal []byte.

func (*License) Verify

func (l *License) Verify(k *PublicKey) (bool, error)

Verify the License with the public key using SM2

type PrivateKey

type PrivateKey struct {
	// contains filtered or unexported fields
}

PrivateKey is the master key to create the licenses. Keep it in a secure location.

func NewPrivateKey

func NewPrivateKey() (*PrivateKey, error)

NewPrivateKey generates a new SM2 private key.

func PrivateKeyFromB32String

func PrivateKeyFromB32String(str string) (*PrivateKey, error)

PrivateKeyFromB32String returns a private key from a base32 encoded string.

func PrivateKeyFromB64String

func PrivateKeyFromB64String(str string) (*PrivateKey, error)

PrivateKeyFromB64String returns a private key from a base64 encoded string.

func PrivateKeyFromBytes

func PrivateKeyFromBytes(b []byte) (*PrivateKey, error)

PrivateKeyFromBytes returns a private key from a []byte.

func PrivateKeyFromHexString

func PrivateKeyFromHexString(str string) (*PrivateKey, error)

PrivateKeyFromHexString returns a private key from a hexadecimal encoded string.

func (*PrivateKey) GetPublicKey

func (k *PrivateKey) GetPublicKey() *PublicKey

GetPublicKey returns the PublicKey associated with the private key.

func (*PrivateKey) ToB32String

func (k *PrivateKey) ToB32String() (string, error)

ToB32String transforms the private key to a base32 string.

func (*PrivateKey) ToB64String

func (k *PrivateKey) ToB64String() (string, error)

ToB64String transforms the private key to a base64 string.

func (*PrivateKey) ToBytes

func (k *PrivateKey) ToBytes() ([]byte, error)

ToBytes transforms the private key to a []byte.

func (*PrivateKey) ToHexString

func (k *PrivateKey) ToHexString() (string, error)

ToHexString transforms the private key to a hexadecimal string

type PublicKey

type PublicKey struct {
	X *big.Int
	Y *big.Int
}

PublicKey is used to check the validity of the licenses. You can share it freely. It uses SM2 curve (sm2p256v1).

func PublicKeyFromB32String

func PublicKeyFromB32String(str string) (*PublicKey, error)

PublicKeyFromB32String returns a public key from a base32 encoded string.

func PublicKeyFromB64String

func PublicKeyFromB64String(str string) (*PublicKey, error)

PublicKeyFromB64String returns a public key from a base64 encoded string.

func PublicKeyFromBytes

func PublicKeyFromBytes(b []byte) (*PublicKey, error)

PublicKeyFromBytes returns a public key from a []byte. 支持未压缩格式 (04 || X || Y) 的公钥

func PublicKeyFromHexString

func PublicKeyFromHexString(str string) (*PublicKey, error)

PublicKeyFromHexString returns a public key from a hexadecimal encoded string.

func (*PublicKey) ToB32String

func (k *PublicKey) ToB32String() string

ToB32String transforms the public key to a base32 string.

func (*PublicKey) ToB64String

func (k *PublicKey) ToB64String() string

ToB64String transforms the public key to a base64 string.

func (*PublicKey) ToBytes

func (k *PublicKey) ToBytes() []byte

ToBytes transforms the public key to a []byte.

func (*PublicKey) ToHexString

func (k *PublicKey) ToHexString() string

ToHexString transforms the public key to a hexadecimal string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL