common

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FFGNetPubSubBlocksTXQuery is a pub sub topic name to receive transactions, data queries and blocks.
	FFGNetPubSubBlocksTXQuery = "ffgnet_pubsub"

	// FFGNetPubSubStorageQuery is a pub sub topic name to receive storage queryies.
	FFGNetPubSubStorageQuery = "ffgnet_pubsub_storage"
)
View Source
const (
	// EncryptionTypeAES key and iv are both 32 bytes for aes
	EncryptionTypeAES256 EncryptionType = 1
	// EncryptionTypeChacha20 key 32 bytes, iv(nounce) 32 bytes
	EncryptionTypeChacha20 EncryptionType = 2

	// KB represents 1024 bytes
	KB = 1024

	// MB represents 1024 KBytes
	MB = 1024 * 1024
)

Variables

This section is empty.

Functions

func CalculateFileHosterTotalContractFees added in v1.3.1

func CalculateFileHosterTotalContractFees(downloadContract *messages.DownloadContractProto, fileHosterFeesPerByte *big.Int) (*big.Int, error)

CalculateFileHosterTotalContractFees given a download contract calculates the amount required by the file hoster

func ChunkString

func ChunkString(s string, chunkSize int) []string

ChunkString chunks a string based on the chunk size.

func ConcatenateFiles

func ConcatenateFiles(outputFile string, inputFiles []string) error

ConcatenateFiles concatenates multiple files into one output file.

func CreateDirectory

func CreateDirectory(path string) error

CreateDirectory creates a directory.

func DecryptFileSegments

func DecryptFileSegments(fileSize, totalSegments, percentageToEncryptData int, randomizedFileSegments []int, input ReadWriteSeekerSyncer, output io.WriteCloser, encryptor DataEncryptor, onlyFileReArrangement bool) error

DecryptFileSegments decrypts the segments and replaces them in the original file and then performs a file block/segment re-arrangement and writes to the output.

func DefaultDataDir

func DefaultDataDir() string

DefaultDataDir returns the default datadir.

func DirExists

func DirExists(filename string) bool

DirExists checks if destination dir exists

func EncryptWriteOutput

func EncryptWriteOutput(fileSize, from, to, totalSegments, percentageToEncryptData int, randomizedFileSegments []int, input io.ReadSeekCloser, output io.WriteCloser, encryptor DataEncryptor) error

EncryptWriteOutput uses the stream cipher to encrypt the input reader and write to the output.

func FileExists

func FileExists(filename string) bool

FileExists checks if destination file exists

func FileSegmentsInfo

func FileSegmentsInfo(fileSize int, howManySegments int, percentageToEncrypt int) (int, int, int, int, error)

FileSegmentsInfo returns the info about the segments of a file and number of encrypted segment given the file size.

func FileSize

func FileSize(fullPath string) (int64, error)

FileSize gets the file size

func FormatBigWithSeperator

func FormatBigWithSeperator(s string, sep string, index int) string

FormatBigWithSeperator formats a big number with separator.

func GenerateRandomIntSlice

func GenerateRandomIntSlice(totalPerm int) []int

GenerateRandomIntSlice generates a random slice we always keep the last item same as original index

func GetDirectoryFreeSpace added in v1.1.20

func GetDirectoryFreeSpace(directoryPath string) (uint64, error)

GetDirectoryFreeSpace returns the available space of a directory.

func GetFileMerkleRootHash

func GetFileMerkleRootHash(filePath string, totalSegments int, segmentsOrder []int) ([]byte, error)

GetFileMerkleRootHash get a merkle root.

func GetFileMerkleRootHashFromNodes

func GetFileMerkleRootHashFromNodes(fileBlockHashes []FileBlockHash) ([]byte, error)

GetFileMerkleRootHashFromNodes get a merkle root from the content.

func HomeDir

func HomeDir() string

HomeDir returns the home directory.

func IsValidPath added in v1.2.1

func IsValidPath(s string) bool

IsValidPath checks if path contains more than one "." character.

func LeftPad2Len

func LeftPad2Len(s string, padStr string, overallLen int) string

LeftPad2Len add padding deparator.

func Reverse

func Reverse(s string) string

Reverse a string.

func WriteToFile

func WriteToFile(data []byte, filePath string) (string, error)

WriteToFile writes data to a file.

func WriteUnencryptedSegments

func WriteUnencryptedSegments(fileSize, totalSegments, percentageToEncryptData int, randomizedFileSegments []int, input io.ReadSeekCloser, output io.WriteCloser) error

WriteUnencryptedSegments takes the file segments that need to be encrypted and copies them to output before encryption is performed.

Types

type DataEncryptor

type DataEncryptor interface {
	StreamEncryptor() (cipher.Stream, error)
	EncryptionType() EncryptionType
}

DataEncryptor is an interface to define the functionality of a data encryptor.

type EncryptionType

type EncryptionType int32

EncryptionType represents an encryption mechanism.

type Encryptor

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

Encryptor represents an encryptor.

func NewEncryptor

func NewEncryptor(encryptionType EncryptionType, key, iv []byte) (*Encryptor, error)

NewEncryptor is a new encryptor.

func (*Encryptor) EncryptionType

func (e *Encryptor) EncryptionType() EncryptionType

EncryptionType returns the type of the encryptor.

func (*Encryptor) StreamEncryptor

func (e *Encryptor) StreamEncryptor() (cipher.Stream, error)

StreamEncryptor gets the encryptor.

type FileBlockHash

type FileBlockHash struct {
	X []byte
}

FileBlockHash represents a hash of a block (range of bytes) of a file.

func EncryptAndHashSegments

func EncryptAndHashSegments(fileSize, totalSegments int, randomizedFileSegments []int, input io.ReadSeekCloser, encryptor DataEncryptor) ([]FileBlockHash, error)

EncryptAndHashSegments encrypts a file's raw segment given a key and hashes the segment.

func HashFileBlockSegments

func HashFileBlockSegments(filePath string, totalSegments int, randomSegments []int) ([]FileBlockHash, error)

HashFileBlockSegments hashes all the file block segments and returns the merkle tree nodes. default totalSegments is 1024

func RetrieveMerkleTreeNodesFromFileWithRawData

func RetrieveMerkleTreeNodesFromFileWithRawData(encryptEverySegment int, randomizedFileSegments []int, merkleTreeItems, merkleTreeOfRawSegments []FileBlockHash) ([]FileBlockHash, error)

RetrieveMerkleTreeNodes retrives the original order of merkle tree given the random list.

func (FileBlockHash) CalculateHash

func (f FileBlockHash) CalculateHash() ([]byte, error)

CalculateHash hashes the content of FileBlockHash.x.

func (FileBlockHash) Equals

func (f FileBlockHash) Equals(other merkletree.Content) (bool, error)

Equals tests for equality of two FileBlockHash.

type FileBlockRange

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

FileBlockRange represents range of bytes to be encrypted.

func PrepareFileBlockRanges

func PrepareFileBlockRanges(from, to, fileSize, totalSegments, segmentSizeBytes, totalSegmentsToEncrypt, encryptEverySegment int, randomSlice []int) ([]FileBlockRange, bool)

PrepareFileBlockRanges returns a list of file block/segments and the order for each block. random slice is used to shuffle the byte blocks/segments.

type ReadWriteSeekerSyncer

type ReadWriteSeekerSyncer interface {
	io.ReadWriteSeeker
	Sync() error
}

ReadWriteSeekerSyncer is used to add Sync to ReadWriteSeeker interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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