signature

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: Apache-2.0 Imports: 11 Imported by: 1

README

go-signature

GitHub tag (latest SemVer) Go Reference License

Tests CodeQL Analysis GolangCI Lint Go Report Card

Just a small library to get a signed string with a payload. It helps me to create confirmation tokens without using database.

Don't use this library to protect sensitive data!

Features

  • Data Integrity: Ensures that the data remains unchanged and secure during transit.
  • Simplified Token Structure: Generates tokens without the overhead of JWT headers, focusing solely on payload and signature.
  • Flexibility and Ease of Use: Provides a straightforward API to work with, requiring minimal setup to sign and verify data.

Usage

Installation:
go get -u github.com/dmitrymomot/go-signature
Set up signing key
import signature "github.com/dmitrymomot/go-signature"

...
signature.SetSigningKey("secret-key")
...

Signing key will be set globally, so you don't need defining it each times

Create signed string:
import signature "github.com/dmitrymomot/go-signature"

...
signedString, _ := signature.New("some data of any type")
log.Println(signedString)
...

Output:

eyJwIjoic29tZSBkYXRhIG9mIGFueSB0eXBlIn0.MzYwMzA0ZGVhNWRmMjdjOTM0ZjY1NzU3YWUwM2I0MDZmODRiMzRiMw
Parse signed string:
data, err := signature.Parse("eyJwIjoic29tZSBkYXRhIG9mIGFueSB0eXBlIn0.MzYwMzA0ZGVhNWRmMjdjOTM0ZjY1NzU3YWUwM2I0MDZmODRiMzRiMw")
if err != nil {
    panic(err)
}
log.Println(data)

Output:

some data of any type

Examples

You can find more examples in the example/main.go file.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSignature           = errors.New("invalid signature")
	ErrorCalculatingHmac          = errors.New("error calculating hmac")
	ErrFailedToMarshalTokenClaims = errors.New("failed to marshal token claims")
	ErrInvalidToken               = errors.New("invalid token")
	ErrTokenExpired               = errors.New("token expired")
	ErrInvalidTokenFormat         = errors.New("invalid token format")
)

Predefined errors

Functions

func New

func New[Payload any](data Payload) (string, error)

New returns sha1-signed string without expiration time

func New256

func New256[Payload any](data Payload) (string, error)

New256 returns sha256-signed string without expiration time.

func New256Temporary

func New256Temporary[Payload any](data Payload, ttl time.Duration) (string, error)

New256Temporary returns sha256-signed string with expiration time ttl - token life time in seconds

func NewTemporary

func NewTemporary[Payload any](data Payload, ttl time.Duration) (string, error)

NewTemporary returns sha1-signed string with expiration time ttl - token life time in seconds

func Parse

func Parse[Payload any](token string) (Payload, error)

Parse token signed by sha1

func Parse256

func Parse256[Payload any](token string) (Payload, error)

Parse256 parses sha256-signed token

func SetSigningKey

func SetSigningKey(sk string)

SetSigningKey helper to set up global signing key

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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