Skip to content

Boltdb wrapper to securely store and retrieve the values via AES encrypt and decrypt function

License

Notifications You must be signed in to change notification settings

linkthings/boltsec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 and etc.

Boltdb file is always open in the file system unless the 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 db 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

var err error
bucketName := "article"

dbm, err := NewDBManager("test.dat", "example", "secret", false, []string{bucketName})

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

if err=dbm.Save(bucketName, data.ID, data); err != nil {
	t.Errorf("TestDBMCreate save data return err: %s", err)
}

var bytes []byte
if bytes, err = dbm.GetOne(bucketName, data.ID); err != nil {
	t.Errorf("TestDBMCreate GetOne return err: %s", err)
}

resNew := new(Article)
if err = json.Unmarshal(bytes, resNew); err != nil {
    t.Errorf("json.Unmarshal return err: %s", err)
}

if resNew.Title != data.Title {
	t.Errorf("returned Title is not equal: new:%s, org: %s", resNew.Title, data.Title)
}

if err=dbm.Delete(bucketName, data.ID); err != nil {
	t.Errorf("TestDBMCreate Delete return err: %s", err)
}

A example code can be found in example folder on how to utilize this package

About

Boltdb wrapper to securely store and retrieve the values via AES encrypt and decrypt function

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages