es

package module
v0.0.0-...-8bd6cd9 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

ephemeral secret - ECDHE based on Curve25519 meets XChaCha20-Poly1305 AEAD.

This package provides functions to easily generate keys to en-/decrypt data through a derived key of the consensus secret from the used ephemeral keys (private/public). The knowledge of the senders (private) and receivers (public) keys are the basic information to exchange data with encrypted messages.

Structure: Version[2], Sender's Public Key[32], Ciphertext[NonceSizeX[24]+Overhead[16]+Payload[x]], Hash[8]

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Encoder Encoding = defaultEncoder
	Decoder Decoding = defaultDecoder
)

Functions

func New

func New() (Key, *Message, error)

New creates a new private Key and an empty Message for initial usage with no reply anticipation.

func Salt

func Salt() ([]byte, error)

Salt returns 32 random bytes.

Types

type Compression

type Compression func(in io.Reader, out io.Writer) error
var (
	Compressor   Compression = nil
	Decompressor Compression = nil
)

type Decoding

type Decoding func(code []byte) []byte

type Encoding

type Encoding func(key []byte) []byte

type Key

type Key struct {
	// contains filtered or unexported fields
}

func NewKey

func NewKey() (Key, error)

NewKey generates a new random private key.

func Parse

func Parse(in string, kind Kind) (Key, error)

Parse retruns a Key from an encoded string.

func PrivateKey

func PrivateKey(key []byte) (Key, error)

PrivateKey converts the given key to a Private Key.

func PublicKey

func PublicKey(key []byte) (Key, error)

PublicKey converts the given key to a Public Key.

func (*Key) Bytes

func (k *Key) Bytes() []byte

Bytes retruns a byte slice of the given Key.

func (*Key) Is

func (k *Key) Is() Kind

Is retruns the Kind (Private/Public) of the given Key.

func (*Key) Public

func (k *Key) Public() (Key, error)

Public calculates the corresponding public key if the given Key is a private key else it returns a copy.

func (*Key) String

func (k *Key) String() string

String retruns an encoded string of the given Key.

type Kind

type Kind uint8
const (
	Private Kind = iota
	Public
)

type Message

type Message struct {
	// contains filtered or unexported fields
}

func NewMessage

func NewMessage() *Message

NewMessage generates a new empty message.

func (*Message) Bytes

func (m *Message) Bytes() []byte

Bytes returns the encrypted data from a Message.

func (*Message) Compress

func (m *Message) Compress(c Compression)

Compress sets c as Compression method to compress the payload of that Message.

func (*Message) Decompress

func (m *Message) Decompress(d Compression)

Decompress sets d as Compession method to decompress the payload of that Message.

func (*Message) Decrypt

func (m *Message) Decrypt(private Key, salt []byte) ([]byte, error)

Decrypt Message's data for receivers private Key and return the decrypted data.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/es"
)

var (
	Receiver es.Key

	M    *es.Message
	Salt []byte
)

func main() {
	// decrypt for Receiver (private key) the Message
	pt, err := M.Decrypt(Receiver, Salt)
	if err != nil {
		log.Fatal(err)
	}

	// print first 11 bytes
	fmt.Println(string(pt[:11]))
}
Output:

Lorem ipsum

func (*Message) Encrypt

func (m *Message) Encrypt(private, public Key, data []byte, salt []byte) error

Encrypt the given data for receiver public Key with senders private Key.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/es"
)

var (
	Data   []byte = []byte("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.")
	Sender []byte = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

	ReceiverPublic es.Key
	M              *es.Message
	Salt           []byte
)

func main() {
	// use senders private key
	sender, err := es.PrivateKey(Sender)
	if err != nil {
		log.Fatal(err)
	}

	// encrypt Data in to Message for Receiver (public key)
	err = M.Encrypt(sender, ReceiverPublic, Data, Salt)
	if err != nil {
		log.Fatal(err)
	}

	// ciphertext is 673 bytes long
	fmt.Println(len(M.Bytes()))
}
Output:

673

func (*Message) Method

func (m *Message) Method() byte

Method returns the method value from a Message.

func (*Message) Payload

func (m *Message) Payload(payload []byte)

Payload adds/overwrites data with the given payload.

func (*Message) Public

func (m *Message) Public() Key

func (*Message) Purge

func (m *Message) Purge()

Purge resets the given Message type.

func (*Message) String

func (m *Message) String() string

String returns the encrypted data from a Message in a base91 encoded string. If the Message is empty or an encoding error occurs, the returned string will be empty.

func (*Message) Version

func (m *Message) Version() byte

Version returns the version from a Message.

type Version

type Version struct {
	// contains filtered or unexported fields
}

func (Version) Bytes

func (v Version) Bytes() []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳