safelock

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2025 License: AGPL-3.0 Imports: 20 Imported by: 1

Documentation

Overview

Fast files encryption package ⚡

Check encryption and decryption for interactive examples.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArchiverConfig added in v0.4.0

type ArchiverConfig struct {
	// files compression (default: zstd.SpeedFastest)
	Compression archiver.Compression
	// files archiving (default: tar)
	Archival archiver.Archival
}

archiving and compression configuration settings

type EncryptionConfig added in v0.4.0

type EncryptionConfig struct {
	// encryption key length (default: 32)
	KeyLength uint32
	// encryption salt length (default: 16)
	SaltLength int
	// number of argon2 hashing iterations (default: 3)
	IterationCount uint32
	// memory allocated for generating argon2 key (default: 64 * 1024)
	MemSize uint32
	// number of threads used to generate argon2 key (default: runtime.NumCPU())
	Threads uint8
	// minimum password length allowed (default: 8)
	MinPasswordLength int
	// ratio to create file header size based on (default: 1024 * 4)
	HeaderRatio int
	// contains filtered or unexported fields
}

encryption/decryption configuration settings

type InputReader added in v0.4.0

type InputReader interface {
	io.Reader
	io.Seeker
}

type Safelock

type Safelock struct {
	EncryptionConfig
	ArchiverConfig

	// disable all output and logs (default: false)
	Quiet bool
	// observable instance that allows us to stream the status to multiple listeners
	StatusObs *StatusObservable
}

the main object used to configure safelock

func New

func New() *Safelock

creates a new safelock.Safelock instance with the default recommended options

func (*Safelock) Decrypt

func (sl *Safelock) Decrypt(ctx context.Context, input InputReader, outputPath, password string) (err error)

decrypts `input` which must be an object that implements io.Reader and io.Seeker such as os.File and then outputs the content into `outputPath` which must be a valid path to an existing directory

NOTE: `ctx` context is optional you can pass `nil` and the method will handle it

Example
package main

import (
	"context"
	"fmt"
	"os"
	"path/filepath"

	"github.com/mrf345/safelock-cli/safelock"
)

func main() {
	lock := safelock.New()
	password := "testing123456"
	ctx := context.Background()

	// Disable logs and output
	lock.Quiet = true

	// Prepare files to decrypt and clean up after test
	encryptedFile := getEncryptedFile(password)
	outputPath := filepath.Dir(encryptedFile.Name())
	defer os.Remove(encryptedFile.Name())
	defer os.RemoveAll(outputPath)

	// This will decrypt `encryptedFile` and extract files into `outputFile`
	if err := lock.Decrypt(ctx, encryptedFile, outputPath, password); err != nil {
		fmt.Println("failed!")
	}

}

func getEncryptedFile(password string) (outputFile *os.File) {
	lock := safelock.New()
	lock.Quiet = true
	ctx := context.TODO()

	inputFile, _ := os.CreateTemp("", "test_input")
	filePaths := []string{inputFile.Name()}
	outputFile, _ = os.CreateTemp("", "test_output")

	_ = lock.Encrypt(ctx, filePaths, outputFile, password)

	return
}

func (*Safelock) Encrypt

func (sl *Safelock) Encrypt(ctx context.Context, inputPaths []string, output io.Writer, password string) (err error)

encrypts `inputPaths` which can be either a slice of file or directory paths and then outputs into an object `output` that implements io.Writer such as io.File

NOTE: `ctx` context is optional you can pass `nil` and the method will handle it

Example
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/mrf345/safelock-cli/safelock"
)

func main() {
	lock := safelock.New()
	password := "testing123456"
	ctx := context.Background()

	// Disable logs and output
	lock.Quiet = true

	// Increase minimum password length requirement
	lock.MinPasswordLength = 12

	// Prepare files to encrypt and clean up after test
	inputFile, _ := os.CreateTemp("", "test_input")
	outputFile, _ := os.CreateTemp("", "test_output")
	inputPaths := []string{inputFile.Name()}
	defer os.Remove(outputFile.Name())
	defer os.Remove(inputFile.Name())

	// This will encrypt all `inputPaths` and write into `outputFile`
	if err := lock.Encrypt(ctx, inputPaths, outputFile, password); err != nil {
		fmt.Println("failed!")
	}

}

type StatusEvent added in v0.4.0

type StatusEvent string

safelock.Safelock.StatusObs streaming event keys type

const (
	StatusStart  StatusEvent = "start_status"  // encryption/decryption has started
	StatusEnd    StatusEvent = "end_status"    // encryption/decryption has ended
	StatusUpdate StatusEvent = "update_status" // new status update
	StatusError  StatusEvent = "error_status"  // encryption/decryption failed
)

safelock.Safelock.StatusObs streaming event keys

func (StatusEvent) Str added in v0.4.0

func (se StatusEvent) Str() string

return event key value as string

type StatusItem added in v0.6.0

type StatusItem struct {
	// status change event key
	Event StatusEvent
	// completion percent
	Percent float64
	// optional status change text
	Msg string
	// optional status change error
	Err error
}

item used to communicate status changes

type StatusObservable added in v0.6.0

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

observable like data structure used to stream status changes

func NewStatusObs added in v0.6.0

func NewStatusObs() *StatusObservable

creates a new safelock.StatusObservable instance

func (*StatusObservable) Subscribe added in v0.6.0

func (obs *StatusObservable) Subscribe(callback func(StatusItem)) func()

adds a new status change subscriber, and returns the unsubscribe function

func (*StatusObservable) Unsubscribe added in v0.6.0

func (obs *StatusObservable) Unsubscribe()

clears all subscriptions

Jump to

Keyboard shortcuts

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