gopeer

package module
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2021 License: MIT Imports: 20 Imported by: 0

README

gopeer

Framework for create decentralized networks. Version: 1.2.7s.

Framework based applications
Research Article
Specifications
  • Type: Embedded;
  • Protocol: TCP;
  • Routing: Fill;
  • Encryption: E2E;
  • Symmetric algorithm: AES-CBC;
  • Asymmetric algorithm: RSA-OAEP, RSA-PSS;
  • Hash function: SHA256;
Template
package main

import (
    gp "./gopeer"
)

func init() {
    gp.Set(gp.SettingsType{
        "AKEY_SIZE": uint(1 << 10),
        "SKEY_SIZE": uint(1 << 4),
    })
}

func main() {
    gp.NewClient(
        gp.GenerateKey(gp.Get("AKEY_SIZE").(uint)), 
        handleFunc,
    ).RunNode(":8080")
    // ...
}

func handleFunc(client *gp.Client, pack *gp.Package) {
    // ...
}
Settings
type SettingsType map[string]interface{}
type settingsStruct struct {
    END_BYTES string
    ROUTE_MSG string
    RETRY_NUM uint
    WAIT_TIME uint
    POWS_DIFF uint
    CONN_SIZE uint
    BUFF_SIZE uint
    PACK_SIZE uint
    MAPP_SIZE uint
    AKEY_SIZE uint
    SKEY_SIZE uint
    RAND_SIZE uint
}
Default settings
{
    END_BYTES: "\000\005\007\001\001\007\005\000",
    ROUTE_MSG: "\000\001\002\003\004\005\006\007",
    RETRY_NUM: 3,       // quantity
    WAIT_TIME: 20,      // seconds
    POWS_DIFF: 20,      // bits
    CONN_SIZE: 10,      // quantity
    BUFF_SIZE: 2 << 20, // 2*(2^20)B = 2MiB
    PACK_SIZE: 4 << 20, // 4*(2^20)B = 4MiB
    MAPP_SIZE: 2 << 10, // 2*(2^10)H = 88KiB
    AKEY_SIZE: 2 << 10, // 2*(2^10)b = 256B
    SKEY_SIZE: 1 << 5,  // 2^5B = 32B
    RAND_SIZE: 1 << 4,  // 2^4B = 16B
}
Settings functions
func Set(settings SettingsType) []uint8 {}
func Get(key string) interface{} {}
Get/Set settings example
var AKEY_SIZE = gopeer.Get("AKEY_SIZE").(uint)
gopeer.Set(gopeer.SettingsType{
    "AKEY_SIZE": uint(1 << 10),
    "SKEY_SIZE": uint(1 << 4),
})
Network functions and methods
func NewClient(priv *rsa.PrivateKey, handle func(*Client, *Package)) *Client {}
func NewPackage(title, data string) *Package {}
func (client *Client) Handle(title string, pack *Package, handle func(*Client, *Package) string) {}
func (client *Client) RunNode(address string) error {}
func (client *Client) Send(receiver *rsa.PublicKey, pack *Package, route []*rsa.PublicKey, ppsender *rsa.PrivateKey) (string, error) {}
func (client *Client) RoutePackage(receiver *rsa.PublicKey, pack *Package, route []*rsa.PublicKey, ppsender *rsa.PrivateKey) *Package {}
func (client *Client) Connections() []string {}
func (client *Client) InConnections(address string) bool {}
func (client *Client) Connect(address string) error {}
func (client *Client) Disconnect(address string) {}
func (client *Client) Encrypt(receiver *rsa.PublicKey, pack *Package) *Package {}
func (client *Client) Decrypt(pack *Package) *Package {}
func (client *Client) PublicKey() *rsa.PublicKey {}
func (client *Client) PrivateKey() *rsa.PrivateKey {}
func (f2f *friendToFriend) State() bool {}
func (f2f *friendToFriend) Switch() {}
func (f2f *friendToFriend) List() []rsa.PublicKey {}
func (f2f *friendToFriend) InList(pub *rsa.PublicKey) bool {}
func (f2f *friendToFriend) Append(pub *rsa.PublicKey) {}
func (f2f *friendToFriend) Remove(pub *rsa.PublicKey) {}
Cryptography functions
func GenerateBytes(max uint) []byte {}
func GenerateKey(bits uint) *rsa.PrivateKey {}
func HashSum(data []byte) []byte {}
func HashPublicKey(pub *rsa.PublicKey) string {}
func BytesToPrivateKey(privData []byte) *rsa.PrivateKey {}
func BytesToPublicKey(pubData []byte) *rsa.PublicKey {}
func StringToPrivateKey(privData string) *rsa.PrivateKey {}
func StringToPublicKey(pubData string) *rsa.PublicKey {}
func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte {}
func PublicKeyToBytes(pub *rsa.PublicKey) []byte {}
func PrivateKeyToString(priv *rsa.PrivateKey) string {}
func PublicKeyToString(pub *rsa.PublicKey) string {}
func EncryptRSA(pub *rsa.PublicKey, data []byte) []byte {}
func DecryptRSA(priv *rsa.PrivateKey, data []byte) []byte {}
func Sign(priv *rsa.PrivateKey, data []byte) []byte {}
func Verify(pub *rsa.PublicKey, data, sign []byte) error {}
func EncryptAES(key, data []byte) []byte {}
func DecryptAES(key, data []byte) []byte {}
func RaiseEntropy(info, salt []byte, bits int) []byte {}
func ProofOfWork(packHash []byte, diff uint) uint64 {}
func ProofIsValid(packHash []byte, diff uint, nonce uint64) bool {}
Additional functions
func SerializePackage(pack *Package) string {}
func DeserializePackage(jsonData string) *Package {}
func Base64Encode(data []byte) string {}
func Base64Decode(data string) []byte {}
Package structure
{
    Head: {
        Rand:    string,
        Title:   string,
        Sender:  string,
        Session: string,
    },
    Body: {
        Data: string,
        Hash: string,
        Sign: string,
        Npow: uint64,
    },
}
Client structure
{
    handle:      func(*Client, *Package)
    mutex:       sync.Mutex,
    privateKey:  *rsa.PrivateKey,
    mapping:     map[string]bool,
    connections: map[string]net.Conn,
    actions:     map[string]chan bool,
    F2F:         {
        mutex:   sync.Mutex,
        enabled: bool,
        friends: map[string]*rsa.PublicKey,
    },
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64Decode

func Base64Decode(data string) []byte

func Base64Encode

func Base64Encode(data []byte) string

func BytesToPrivateKey added in v1.2.6

func BytesToPrivateKey(privData []byte) *rsa.PrivateKey

Used PKCS1.

func BytesToPublicKey added in v1.2.6

func BytesToPublicKey(pubData []byte) *rsa.PublicKey

Used PKCS1.

func DecryptAES

func DecryptAES(key, data []byte) []byte

AES with CBC-mode.

func DecryptRSA

func DecryptRSA(priv *rsa.PrivateKey, data []byte) []byte

Used RSA(OAEP).

func EncryptAES

func EncryptAES(key, data []byte) []byte

AES with CBC-mode.

func EncryptRSA

func EncryptRSA(pub *rsa.PublicKey, data []byte) []byte

Used RSA(OAEP).

func GenerateBytes added in v1.2.0

func GenerateBytes(max uint) []byte

Generates a cryptographically strong pseudo-random sequence.

func GenerateKey added in v1.2.6

func GenerateKey(bits uint) *rsa.PrivateKey

Create private key by number of bits.

func Get

func Get(key string) interface{}

func HashPublicKey added in v1.2.6

func HashPublicKey(pub *rsa.PublicKey) string

HashPublicKey(x) = Base64Encode(HashSum(PublicKeyToBytes(x))).

func HashSum

func HashSum(data []byte) []byte

Used SHA256.

func PrivateKeyToBytes added in v1.2.6

func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte

Used PKCS1.

func PrivateKeyToString added in v1.2.6

func PrivateKeyToString(priv *rsa.PrivateKey) string

PrivateKeyToString(x) = Base64Encode(PrivateKeyToBytes(x)).

func ProofIsValid added in v1.2.1

func ProofIsValid(packHash []byte, diff uint, nonce uint64) bool

Verifies the work of the proof of work function.

func ProofOfWork

func ProofOfWork(packHash []byte, diff uint) uint64

Proof of work by the method of finding the desired hash. Hash must start with 'diff' number of zero bits.

func PublicKeyToBytes added in v1.2.6

func PublicKeyToBytes(pub *rsa.PublicKey) []byte

Used PKCS1.

func PublicKeyToString added in v1.2.6

func PublicKeyToString(pub *rsa.PublicKey) string

PublicKeyToString(x) = Base64Encode(PublicKeyToBytes(pub)).

func RaiseEntropy added in v1.2.6

func RaiseEntropy(info, salt []byte, bits int) []byte

Increase entropy by multiple hashing.

func SerializePackage added in v1.2.2

func SerializePackage(pack *Package) string

Serialize with JSON format.

func Set

func Set(settings SettingsType) []uint8

func Sign

func Sign(priv *rsa.PrivateKey, data []byte) []byte

Used RSA(PSS).

func StringToPrivateKey added in v1.2.6

func StringToPrivateKey(privData string) *rsa.PrivateKey

StringToPrivateKey(x) = BytesToPrivateKey(Base64Decode(x)).

func StringToPublicKey added in v1.2.6

func StringToPublicKey(pubData string) *rsa.PublicKey

StringToPublicKey(x) = BytesToPublicKey(Base64Decode(x)).

func ToBytes

func ToBytes(num uint64) []byte

Uint64 to slice of bytes by big endian.

func Verify

func Verify(pub *rsa.PublicKey, data, sign []byte) error

Used RSA(PSS).

Types

type BodyPackage added in v1.2.0

type BodyPackage struct {
	Data string `json:"data"`
	Hash string `json:"hash"`
	Sign string `json:"sign"`
	Npow uint64 `json:"npow"`
}

type Client

type Client struct {
	F2F *friendToFriend
	// contains filtered or unexported fields
}

Basic structure describing the user. Stores the private key and list of friends.

func NewClient added in v1.2.0

func NewClient(priv *rsa.PrivateKey, handle func(*Client, *Package)) *Client

Create client by private key as identification. Handle function is used when the network exists. Can be null.

func (*Client) Connect

func (client *Client) Connect(address string) error

Connect to node by address. Client handle function need be not null.

func (*Client) Connections

func (client *Client) Connections() []string

Get list of connection addresses.

func (*Client) Decrypt added in v1.2.3

func (client *Client) Decrypt(pack *Package) *Package

Decrypt package with private key of receiver. No one else except the sender will be able to decrypt the package.

func (*Client) Disconnect

func (client *Client) Disconnect(address string)

Disconnect from node by address.

func (*Client) Encrypt added in v1.2.3

func (client *Client) Encrypt(receiver *rsa.PublicKey, pack *Package) *Package

Encrypt package with public key of receiver. The package can be decrypted only if private key is known.

func (*Client) Handle added in v1.2.5

func (client *Client) Handle(title string, pack *Package, handle func(*Client, *Package) string)

Handle package by title. If title equal title in package then go to handle function.

func (*Client) InConnections

func (client *Client) InConnections(address string) bool

Check the existence of an address in the list of connections.

func (*Client) PrivateKey added in v1.2.6

func (client *Client) PrivateKey() *rsa.PrivateKey

Get private key from client object.

func (*Client) PublicKey added in v1.2.6

func (client *Client) PublicKey() *rsa.PublicKey

Get public key from client object.

func (*Client) RoutePackage added in v1.2.6

func (client *Client) RoutePackage(receiver *rsa.PublicKey, pack *Package, route []*rsa.PublicKey, ppsender *rsa.PrivateKey) *Package

Function wrap package in multiple route. Need use pseudo sender if route not null.

func (*Client) RunNode added in v1.2.5

func (client *Client) RunNode(address string) error

Turn on listener by address. Client handle function need be not null.

func (*Client) Send added in v1.2.0

func (client *Client) Send(receiver *rsa.PublicKey, pack *Package, route []*rsa.PublicKey, ppsender *rsa.PrivateKey) (string, error)

Send package by public key of receiver. Function supported multiple routing with pseudo sender.

type HeadPackage added in v1.2.0

type HeadPackage struct {
	Rand    string `json:"rand"`
	Title   string `json:"title"`
	Sender  string `json:"sender"`
	Session string `json:"session"`
}

type Package

type Package struct {
	Head HeadPackage `json:"head"`
	Body BodyPackage `json:"body"`
}

func DeserializePackage added in v1.2.2

func DeserializePackage(jsonData string) *Package

Deserialize with JSON format.

func NewPackage added in v1.2.3

func NewPackage(title, data string) *Package

Create package: Head.Title = title, Body.Data = data.

type SettingsType

type SettingsType map[string]interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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