pengine

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package pengine low-level APIs for paket.

Before using it, you need to create a file with the cmd tool. (If you are not creating a new tool or API).

Users do not need functions and structures other than New and Paket methods.

Other exported functions and variables are for the cmd tool. If you only want to read the package created with the cmd tool, you can create a new package method with New().

Index

Constants

This section is empty.

Variables

View Source
var (
	// If there is no data in the map sent to New, the functions you use will return this error.
	MinimumMapValueError = errors.New("map cannot be less than 1 in length")
)

Functions

func CreateRandomBytes

func CreateRandomBytes(l uint8) ([]byte, error)

CreateRandomBytes generates random bytes of the given size. The maximum value should be 32 and the minimum value should be 16.

Used to generate a random key if the user has not specified a key. (for cmd tool)

Returns error for the wrong size or creating bytes.

func Decrypt

func Decrypt(key, data []byte) ([]byte, error)

Decrypt decrypts the encrypted data with the key.

Uses the CFB mode.

It doesn't matter whether you have the correct key or not. It decrypts data with the key given under any condition. So you should compare it with the original data with a suitable hash function (see sha256, sha512 module...). Otherwise, you can't be sure it is returning the correct data.

If everything is working correctly, it returns decrypted bytes and nil error.

func Encrypt

func Encrypt(key, data []byte) ([]byte, error)

Encrypt encrypts the data using the key.

Uses the CFB mode.

Key must be 16, 24 or 32 size. Otherwise, the cypher module returns an error.

You can compare the data sended to the function with the output data. It might be a good idea to make sure it's working properly.

If everything is working correctly, it returns an encrypted bytes and nil error.

func Exists

func Exists(name string) bool

a guarantee about the existence of file.

Source: https://stackoverflow.com/a/12527546/13431469 Thanks to SO user.

Types

type Datas

type Datas map[string]Values

type definition for the Paket.

Paket reads the requested file through this map.

string refers to the file name, values refers to information about the file (length, sha value etc.).

type Paket

type Paket struct {
	// Key value for reading the file's data.
	// As a warning, you shouldn't just create a plaintext key.
	Key []byte

	// Map value that keep the information of files in Paket.
	// It must be at least 1 length.
	// Otherwise, panic occurs at runtime.
	//
	// Usually created by the cmd tool.
	Table Datas
	// contains filtered or unexported fields
}

Paket that keeps the information of the file to be read. It should be created with New.

func New

func New(key []byte, paketFileName string, table Datas) (*Paket, error)

New Creates a new Package method. This method should be used to read the files.

key parameter refers to the encryption key. It must be 16, 24 or 32 length. Returns nil and error for keys of incorrect length.

Panic occurs if the specified file does not exist.

table parameter is defined in go file created by the cmd tool. There must be a minimum of 1 file in the table.

After getting all the data you need, should be terminated with Close.

func (*Paket) Close

func (p *Paket) Close() error

Close Closes the opened file (see Paket.file (non-exported)).

Use this function when all your transactions are done (so you shouldn't use it with defer or something like that). Otherwise, you must create a new Paket method.

When you call Close, you cannot access the Package again.

Returns error for unsuccessful events.

func (*Paket) GetFile

func (p *Paket) GetFile(filename string, decrypt, shaControl bool) ([]byte, bool, error)

GetFile Returns the content of the requested file.

If the file cannot be found in the map and the length cannot be read, a panic occurs.

All errors except these errors return with error.

If decrypt is true, it is decrypted. If not, encrypted bytes are returned.

If value of shaControl is true, the hash of the decrypted data is compared with hash of the original file.

If decrypt is false and shaControl is true, the hash of the encrypted file in the table is compared with the encrypted hash of the read file.

If the hash comparison is true, the second value is set to true.

If hashControl is false, checks are skipped. Returns False.

Both values do not have to be true. However, it may be good to generate a control mechanism like hash with your own work. The decrypt (bool) value has been added for convenience. As a recommendation, it is better to pass both values to true to this function.

func (*Paket) GetGoroutineSafe added in v0.1.0

func (p *Paket) GetGoroutineSafe(name string) ([]byte, error)

GetGoroutineSafe created to securely retrieve data when using with multiple goroutines. In any case, it only returns decrypted data.

It does not do any hash checking.

func (*Paket) GetLen

func (p *Paket) GetLen() ([2]int, error)

GetLen Returns the original and encrypted lengths of all files contained in Paket. 0 index refers to the original, 1 index to the encrypted data. In the meantime, no control is made. The same will return as the values are written into the table.

Normally values should be in bytes.

returns an error if length is less than 1(see MinimumMapValueError). This case, other things are 0.

type Values

type Values struct {
	// start position
	StartPos int

	// end position
	EndPos int

	// length of the original file.
	OriginalLenght int

	// length of the encrypted data.
	EncryptLenght int

	// Hash of the original file.
	HashOriginal string

	// Hash of encrypted data.
	HashEncrypt string
}

type declaration for map values.

Jump to

Keyboard shortcuts

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