task

package
v0.0.0-...-544ea0f Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTask

func AddTask(task string)

AddTask accepts a string and stores it into an existing boltdb database. All the tasks are contained into a bucket named "Tasks". Since boltdb is a k/v store, the key of each task is a number, and the value is the task itself. The key and the value is stored as a slice of bytes. Additionally, the NextSequence() method is used which returns an autoincrementing int to track the number of all the tasks ever added in the database. That int servers as an index which is used as the key for each task added in the database.

Implementation details: - itob is used to convert the id into a byte representation. - The id has to be smaller than math.MaxInt32 to avoid int overflow. - The given task cannot be emtpy.

func DbDecrypt

func DbDecrypt()

dbDecrypt decrypts the file where the boltdb database is located. It initialy loads the encrypted file into the memory as well as the key.if it does not exist then regPassword is called. wrapDataEncrypt contains the desired actions for the function. After the decrypted data is returned it is written back to the filesystem at the path provided by path.DB.

Implementation details:

  • DbDecrypt mainly acts as a second layer of wrapping around the encryption methods, with some additional error handlind.

func DbEncrypt

func DbEncrypt()

dbEncrypt encrypts the file where the boltdb database exists. It initialy loads the db into the memory as well as the key. wrapDataEncrypt contains the desired actions for this function. After the encrypted data is returned it is written back to the filesystem at the path provided by path.DB.

Implementation details:

  • DbEncrypt mainly acts as a second layer of wrapping around the encryption methods.
  • Should be called with defer in each function that interacts with the db.

func DeleteTask

func DeleteTask(taskNum string)

DeleteTask deletes a task based on the index number provided. The only bucket to ever exist in the database is "Tasks" so the deletion only takes place in that bucket. When a task gets deleted, the keys of the tasks following the deleted task get decremented by 1.

Implementation details:

  • If the id number is "" or < 1 then the deletion is skipped.
  • nextKey and nextVal are better names for key and val since cursor.Next() is executed before the first loop takes place.

func HashPassword

func HashPassword(password []byte) []byte

HashPassword returns a byte slice of the sha256 hash created from the password inputed by the user.

Implementation details: - It does not salt the password, it just returns the sha256 equivalent hash.

func ListTasks

func ListTasks()

ListTasks iterates over all the keys and values within the boltdb database and prints each key and each value. The key is converted from []byte to int in order to be printed.

Implementation details:

  • Returning nil in the ForEach method is like a loop's continue statement. If err is returned then the iteration is stopped and the error is returned to the caller.

func ReadPassword

func ReadPassword(newDb bool, pr PasswordReader) (password []byte, err error)

ReadPassword receives the inputed user password from wrapPasswordReader. It accepts a bool var that informs the function if this is a new database. If true then the user is prompted to confirm the previously given password. If the confirmation fails an error is returned.

Implementation details:

  • wrapPasswordReader will call whichever ReadPassword method was passed to it altering how the password is fetched. However, the validity checks of the given password should be contained in this function.

Types

type DB

type DB struct {
}

DB represents an abstraction over a db type. It's only used temporarily to create the needed encrypt and decrypt methods. In future version of the code this type should encompass the boltdb db type and extend it with encryption methods.

func (DB) Decrypt

func (db DB) Decrypt(encryptedData []byte, key []byte) ([]byte, error)

Decrypt is the method that decrypts the data passed as an argument and is ultimately responsibel for decrypting the tasks database. Initially a new cipher block gets created with a given key of 32 bytes (AES-256). Based on the initial cipher block, a block cipher wrapped in Galois Counter Mode with the standard nonce length is returned. To decrypt, it is necessary to indicate the nonce value used during the encryption process. This value is saved at the beginning of the file. Open decrypts and authenticates the encrypted data. The data is then returned.

func (DB) Encrypt

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

Encrypt is the method that encrypts the data passed as an argument and is ultimately responsible for encrypting the tasks database. This particular method creates a new cipher block with a given key of 32 bytes (AES-256). Based on the initial cipher block, a block cipher wrapped in Galois Counter Mode with the standard nonce length is returned. After a nonce is created with the proper length, the data is encrypted by calling Seal. The encrypted data is then returned.

type DataEncrypt

type DataEncrypt interface {
	Encrypt(data []byte, key []byte) ([]byte, error)
	Decrypt(data []byte, key []byte) ([]byte, error)
}

DataEncrypt contains the required methods for encryption and decryption

type PasswordReader

type PasswordReader interface {
	ReadPassword() ([]byte, error)
}

PasswordReader returns password read from a reader

type Path

type Path struct {
	DB  string
	KEY string
}

func SetPaths

func SetPaths() (path *Path)

SetPaths sets the default paths to store the db and the secret key. It serves as an initializer which is called in every function that needs to interact with the database or the key.

Implementation details: - /dev/shm/ is mostly available on linux and not available in macOs

type StdinPasswordReader

type StdinPasswordReader struct {
}

StdInPasswordReader represents an stdin password reader

func (StdinPasswordReader) ReadPassword

func (pr StdinPasswordReader) ReadPassword() ([]byte, error)

ReadPassword for StdinPasswordReader reads the password from stdin by using the term package's ReadPassword function which prompts for user input.

Jump to

Keyboard shortcuts

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