sconn

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2021 License: MIT Imports: 1 Imported by: 0

README

sconn

sconn is encrypts and decrypts socket communication with a private key.

// sconn.go

type SConn interface {

    // net.Conn to be used for encryption
	net.Conn

    // read to net.Conn, decrypt msg
	Read([]byte) (int, error)

    // encrypt msg, write to net.Conn
	Write([]byte) (int, error)
}
// sconn.go

// get net.Conn and private key, upgrade net.Conn to SConn
type ConnUpgrader func(net.Conn, []byte) (SConn, error)

example

// server.go

package main

import (
	"crypto/sha256"
	"net"

	"github.com/vompressor/go_sconn/sconn"
	"github.com/vompressor/go_sconn/sconn/stream/chacha20_upgrader"
)

func main() {
	var sc sconn.SConn

	l, err := net.Listen("tcp", "localhost:54777")
	if err != nil {
		panic(err.Error())
	}

	cc, err := l.Accept()
	if err != nil {
		panic(err.Error())
	}

	defer cc.Close()

	// create key
	k := sha256.Sum256([]byte("hello"))

	sc, err = chacha20_upgrader.Upgrade(cc, k[:])
	if err != nil {
		panic(err.Error())
	}
	sc.Write([]byte("hello"))
}

//client.go

package main

import (
	"crypto/sha256"
	"net"

	"github.com/vompressor/go_sconn/sconn"
	"github.com/vompressor/go_sconn/sconn/stream/chacha20_upgrader"
)

func main() {
	var sc sconn.SConn
	cc, err := net.Dial("tcp", "localhost:54777")
	if err != nil {
		panic(err.Error())
	}

	defer cc.Close()

	// create key
	k := sha256.Sum256([]byte("hello"))

	sc, err := chacha20_upgrader.Upgrade(cc, k[:])
	if err != nil {
		panic(err.Error())
	}

	buf := make([]byte, 1024)
	n, _ := sc.Read(buf)

	println(string(buf[:n]))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnUpgrader

type ConnUpgrader func(net.Conn, []byte) (SConn, error)

type SConn

type SConn interface {
	net.Conn
	Read([]byte) (int, error)
	Write([]byte) (int, error)
}

Jump to

Keyboard shortcuts

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