Categorygithub.com/sshdoc/boltsec
modulepackage
0.0.0-20200925015118-a5651816a7e7
Repository: https://github.com/sshdoc/boltsec.git
Documentation: pkg.go.dev

# README

boltsec

A boltdb wrapper to encrypt and decrypt the values stored in the boltdb via AES Cryptor, and also provides common db operations such as GetOne, GetByPrefix, GetKeyList, Save, Delete, etc...

Boltdb file is always open in the file system unless DB.Close() is called, which cause inconvenience if you want to do some file operations to the db file while the program is running. This package provides the parameter: batchMode to control whether to close the db after each db operation, this has performance impact but could be a useful feature.

Features

  1. Support AES to encrypt and decrypt the values
  2. Batch mode option to control whether to close the db after each operation
  3. Initialize db file and cryptor

Performance

The below is the benchmark data for the DB related operations.

BenchmarkDBMOps-8                       2000        1114202 ns/op
BenchmarkDBMOpsBatchMode-8              5000        383995 ns/op
BenchmarkDBMOpsNoEncryption-8           1000        1029510 ns/op

LICENSE

This project is under license MIT

Usage Example

header code
package main

import (
    "encoding/json"
    "fmt"

    "github.com/linkthings/boltsec"
)

type article struct {
    ID            string
    Name          string
}
func main() {
    bucketName := "article"
    dbm, _ := boltsec.NewDBManager("test.dat", "", "secret", false, []string{bucketName})

    data := article{
        ID:   "ID-0001",
        Name: "input with more than 16 characters",
    }

    dbm.Save(bucketName, data.ID, data)

    var bytes []byte
    bytes, _ = dbm.GetOne(bucketName, data.ID)

    resNew := new(article)
    json.Unmarshal(bytes, resNew)

    if resNew.Name == data.Name {
        fmt.Printf("Decrypted data matches origional input:\nDecrypted:%s\nOriginal:%s\n", resNew.Name, data.Name)
    }

    dbm.Delete(bucketName, data.ID)
}

A complete example with error handling and testing can be found in the example folder

# Packages

A sample code to describe how to use the boltsec and use json.Unmarshal to convert the []byte values into Article object.

# Functions

NewDBManager is the main function to initialize the the DB manager for all DB related operations name: the db file name, such as mydb.dat, mytest.db path: the db file's path, can be "" or any other director secret: the secret value if you want to encrypt the values; if you don't want to encrypt the data, simply put it as "" batchMode: to control whether to close the db file after each db operation buckets: the buckets in the db file to be initialized if the db file does not existed.

# Variables

No description provided by the author
The key error messages generated in the package.
The key error messages generated in the package.
The key error messages generated in the package.
No description provided by the author

# Structs

DBManager struct, all fields are not needed to be accessed by other packages.
No description provided by the author