gojwe

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2025 License: MIT Imports: 10 Imported by: 0

README

gojwe

JWE (AES/GCM A256GCM, ChaCha20-Poly1305, XChaCha20-Poly1305) wrapper for Golang.

Install

go get github.com/prongbang/gojwe

How to use

  • Random Secret Key
openssl rand -hex 32
  • Benchmark AES GCM-256
cpu: Apple M4 Pro
BenchmarkAesGcm256Generate-12    	  184796	      5714 ns/op
BenchmarkAesGcm256Parse-12       	  197023	      6048 ns/op
BenchmarkAesGcm256Verify-12      	  198123	      6033 ns/op
  • Benchmark ChaCha20-Poly1305
cpu: Apple M4 Pro
BenchmarkChaCha20Generate-12     	 1000000	      1124 ns/op
BenchmarkChaCha20Parse-12        	 1000000	      1018 ns/op
BenchmarkChaCha20Verify-12       	 1000000	      1004 ns/op
  • Benchmark XChaCha20-Poly1305
cpu: Apple M4 Pro
BenchmarkXChaCha20Generate-12    	  996456	      1211 ns/op
BenchmarkXChaCha20Parse-12       	 1000000	      1085 ns/op
BenchmarkXChaCha20Verify-12      	 1000000	      1087 ns/op
  • New instance AES GCM-256
j := gojwe.New(gojwe.AESGCM256)
  • New instance ChaCha20-Poly1305
j := gojwe.New(gojwe.ChaCha20)
  • New instance XChaCha20-Poly1305
j := gojwe.New(gojwe.XChaCha20)
  • Generate
key, _ := hex.DecodeString("bdacaf398071931518f73917cb0c6f04b3a0ab45ee9cbedc258047a8c149a3e1")

payload := map[string]any{
    "exp": 99999999999,
}
accessToken, err := j.Generate(payload, key)
  • Parse
key, _ := hex.DecodeString("bdacaf398071931518f73917cb0c6f04b3a0ab45ee9cbedc258047a8c149a3e1")

accessToken := "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjk5OTk5OTk5OTk5fQ.rMKkGe6riuLZ3boYiMZsk5xrT7S-7VK6gZmFs1_7kKtVUkpvGatudYI5ZSkwIQ-iJKp2XskCxzn_6fVkCohtUQ"
payload, err := j.Parse(accessToken, key)
  • Verify
key, _ := hex.DecodeString("bdacaf398071931518f73917cb0c6f04b3a0ab45ee9cbedc258047a8c149a3e1")

accessToken := "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjk5OTk5OTk5OTk5fQ.rMKkGe6riuLZ3boYiMZsk5xrT7S-7VK6gZmFs1_7kKtVUkpvGatudYI5ZSkwIQ-iJKp2XskCxzn_6fVkCohtUQ"
valid := j.Verify(accessToken, key)

Documentation

Index

Constants

View Source
const (
	AESGCM256 = "AES-GCM-256"
	ChaCha20  = "ChaCha20"
	XChaCha20 = "XChaCha20"
)

Variables

This section is empty.

Functions

func HMAC added in v1.2.0

func HMAC(header, payload string, key []byte) string

Types

type Header struct {
	Alg string `json:"alg"`
	Enc string `json:"enc"`
	Iv  string `json:"iv"`
	Tag string `json:"tag"`
}

type JWE added in v1.1.0

type JWE interface {
	Generate(payload map[string]any, key []byte) (string, error)
	Verify(token string, key []byte) bool
	Parse(token string, key []byte) (map[string]any, error)
}

func New

func New(alg string) JWE

type JweAesGcm256 added in v1.1.0

type JweAesGcm256 struct {
}

func (*JweAesGcm256) Generate added in v1.1.0

func (j *JweAesGcm256) Generate(payload map[string]any, key []byte) (string, error)

func (*JweAesGcm256) Parse added in v1.1.0

func (j *JweAesGcm256) Parse(token string, key []byte) (map[string]any, error)

func (*JweAesGcm256) Verify added in v1.1.0

func (j *JweAesGcm256) Verify(token string, key []byte) bool

type JweChaCha20 added in v1.2.0

type JweChaCha20 struct {
}

func (*JweChaCha20) Generate added in v1.2.0

func (j *JweChaCha20) Generate(payload map[string]any, key []byte) (string, error)

func (*JweChaCha20) Parse added in v1.2.0

func (j *JweChaCha20) Parse(token string, key []byte) (map[string]any, error)

func (*JweChaCha20) Verify added in v1.2.0

func (j *JweChaCha20) Verify(token string, key []byte) bool

type JweXChaCha20 added in v1.1.0

type JweXChaCha20 struct {
}

func (*JweXChaCha20) Generate added in v1.1.0

func (j *JweXChaCha20) Generate(payload map[string]any, key []byte) (string, error)

func (*JweXChaCha20) Parse added in v1.1.0

func (j *JweXChaCha20) Parse(token string, key []byte) (map[string]any, error)

func (*JweXChaCha20) Verify added in v1.1.0

func (j *JweXChaCha20) Verify(token string, key []byte) bool

type Serialize added in v1.1.0

type Serialize struct {
	Iv     string
	Tag    string
	Cipher string
}

Jump to

Keyboard shortcuts

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