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 ¶
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 ¶
License represents a license with some data and a signature.
func LicenseFromB32String ¶
LicenseFromB32String returns a License from a base64 encoded string.
func LicenseFromB64String ¶
LicenseFromB64String returns a License from a base64 encoded string.
func LicenseFromBytes ¶
LicenseFromBytes returns a License from a []byte.
func LicenseFromHexString ¶
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 ¶
ToB32String transforms the license to a base32 []byte.
func (*License) ToB64String ¶
ToB64String transforms the licence to a base64 []byte.
func (*License) ToHexString ¶
ToHexString transforms the license to a hexadecimal []byte.
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 ¶
PublicKey is used to check the validity of the licenses. You can share it freely. It uses SM2 curve (sm2p256v1).
func PublicKeyFromB32String ¶
PublicKeyFromB32String returns a public key from a base32 encoded string.
func PublicKeyFromB64String ¶
PublicKeyFromB64String returns a public key from a base64 encoded string.
func PublicKeyFromBytes ¶
PublicKeyFromBytes returns a public key from a []byte. 支持未压缩格式 (04 || X || Y) 的公钥
func PublicKeyFromHexString ¶
PublicKeyFromHexString returns a public key from a hexadecimal encoded string.
func (*PublicKey) ToB32String ¶
ToB32String transforms the public key to a base32 string.
func (*PublicKey) ToB64String ¶
ToB64String transforms the public key to a base64 string.
func (*PublicKey) ToHexString ¶
ToHexString transforms the public key to a hexadecimal string.