lcns

package module
v0.0.0-...-eb0c127 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2018 License: MIT Imports: 13 Imported by: 0

README

go-lcns

GoDoc

go-lcns is a license generation and verification library for Go that uses RSA cryptography underneath. I use this for the enterprise edition of Commento.

Usage

A code snippet speaks a thousand words. Check out godoc for more details.

import "github.com/adtac/go-lcns"
// Read the public and private keys from disk. In practice, you'd be doing only
// one of these; the server would read the private key and the client would
// read the public key.
publicKey, err := ReadPublicKey("testfiles/publickey.crt")
privateKey, err := ReadPrivateKey("testfiles/keypair.pem")
// First let's generate licenses with payloads of built-in types (such as int,
// string). Usually, these payloads contain some identifying information about the
// client. You'll be doing the license key generation on the server side, of course.
licenseKeyString, err := GenerateFromPayload(privateKey, "some payload")
// Now on the client side, let's verify the license by making sure the signature
// matches and extract the payload. You'll be doing this on the client side.
payload, err := VerifyAndExtractPayload(publicKey, licenseKeyString)
if err != nil {
  // The license was probably invalid or corrupt.
}

fmt.Println(payload)  // "some payload"

Using custom structs as payloads is possible, too, but you need to first register the struct. Remember to export all the fields!

type foo struct {
  Bar string
  Baz int
}
x := foo{"bar", 100}
// On the server side, you generate the license key.
gob.Register(foo{})
licenseKeyString, err = GenerateFromPayload(privateKey, x)
// On the client side, you verify the license key. Make sure to use the same struct;
// you may get panics, errors, and other monsters otherwise.
gob.Register(foo{})
payload, err = VerifyAndExtractPayload(publicKey, licenseKeyString)
if err != nil {
  // The license was probably invalid or corrupt.
}

// You can cast the empty interface returned by VerifyAndExtractPayload like so.
fooObject := payload.(foo)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateFromPayload

func GenerateFromPayload(privateKey *rsa.PrivateKey, payload interface{}) (string, error)

GenerateFromPayload takes a rsa.PrivateKey and a payload to include in the license and returns a license key string (if there are no errors). While this works without any extra effort for built-in data types (such as int, string), if you want to use custom structs as payload, you'll need to register the struct before calling GenerateFromPayload with the gob.Register function. See example above.

func ReadPrivateKey

func ReadPrivateKey(filename string) (*rsa.PrivateKey, error)

ReadPrivateKey reads a PEM-encoded X.509 RSA private key and returns a rsa.PrivateKey that can be used in GenerateFromPayload to generate a license key from a payload.

func ReadPublicKey

func ReadPublicKey(filename string) (*rsa.PublicKey, error)

ReadPublicKey reads a PEM-encoded X.509 RSA public key and returns a rsa.PublicKey that can be used in VerifyAndExtractPayload to verify a license key and extract the included payload.

func VerifyAndExtractPayload

func VerifyAndExtractPayload(publicKey *rsa.PublicKey, str string) (interface{}, error)

VerifyAndExtractPayload takes a rsa.PublicKey and a license key string generated with GenerateFromPayload to return an empty interface holding the included payload. A nil interface and an error is returned if the license key is invalid or corrupt. You are expected to assert and convert it to your type; remember that this may cause a panic if you convert to a concrete different type than the one you generated the license key with.

Types

This section is empty.

Jump to

Keyboard shortcuts

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