lazyxchacha

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: MIT Imports: 5 Imported by: 1

README

lazyxchacha

Lazy XChaCha20-Poly1305 in golang base on golang.org/x/crypto.

Go Report Card

Algorithm details
  • Key exchange: X25519
  • Encryption: XChaCha20
  • Authentication: Poly1305
Install
go get github.com/prongbang/lazyxchacha
Benchmark
goos: darwin
goarch: arm64
cpu: Apple M4 Pro
BenchmarkEncrypt-12         	 1882269	       621.3 ns/op
BenchmarkDecrypt-12         	 2717778	       432.8 ns/op
BenchmarkEncryptBytes-12    	 2503562	       471.8 ns/op
BenchmarkDecryptBytes-12    	 3929430	       301.2 ns/op
How to use
  • Generate KeyPair
keyPair := lazyxchacha.NewKeyPair()
  • Key Exchange
// Generate KeyPair
clientKp := lazyxchacha.NewKeyPair()
serverKp := lazyxchacha.NewKeyPair()

serverKx := serverKp.Exchange(clientKp.Pk)
clientKx := clientKp.Exchange(serverKp.Pk)
  • Shared Key
serverSharedKey, _ := serverKx.Secret()
clientSharedKey, _ := clientKx.Secret()
  • Encrypt
lazyXchacha := lazyxchacha.New()
sharedKey, _ := clientKx.Secret()
key, _ := hex.DecodeString(sharedKey)
plaintext := "text"
ciphertext, err := lazyXchacha.Encrypt(plaintext, key)
  • Decrypt
lazyXchacha := lazyxchacha.New()
sharedKey, _ := serverKx.Secret()
key, _ := hex.DecodeString(sharedKey)
ciphertext := "f6a1bd8"
plaintext, err := lazyXchacha.Decrypt(ciphertext, key)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyPair

type KeyPair struct {
	Pk string `json:"pk"`
	Sk string `json:"sk"`
}

func NewKeyPair

func NewKeyPair() KeyPair

func (KeyPair) Exchange

func (k KeyPair) Exchange(pk string) KeyPair

func (KeyPair) Secret

func (k KeyPair) Secret() (string, error)

type LazyXChaCha

type LazyXChaCha interface {
	RandomKey() ([]byte, error)
	Encrypt(plaintext string, key []byte) (string, error)
	Decrypt(ciphertext string, key []byte) (string, error)
	EncryptBytes(plaintext []byte, key []byte) ([]byte, error)
	DecryptBytes(ciphertext []byte, key []byte) ([]byte, error)
}

func New

func New() LazyXChaCha

Jump to

Keyboard shortcuts

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