Giter VIP home page Giter VIP logo

boltsec's Introduction

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

boltsec's People

Contributors

linkthings avatar sshdoc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

jempe diegosz sshdoc

boltsec's Issues

json.Marshal asymetry on Save and GetOne?

I'm using protobufs with boltsec and I'm wondering about the proper place to handle Marshal and Unmarshal... Ben Johnson (Bolt author) recommended Protobufs with bolt since it allows you to rename fields, further helping to sidestep many database migration reasons.

Right now, Save does assume that the incoming data should be marshalled as json, meanwhile GetOne will pass back the raw data, so the caller must call json.Unmarshal...

Inside of the example boltsec_test.go you have:

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

and

	resNew := new(Article)
	json.Unmarshal(bytes, resNew) {

Yet json.Marshal is inside of dbm.Save, but no json.Marshal in GetOne.

Do you think it's better to also include json.Unmarshal in GetOne so that both are consistent? Or possibly to remove marshal/unmarshal so that both sides match?

Assuming you like to have marshal/unmarshal embeded inside boltsec (seems more convenient to me!), what do you think of extending it to cover marshal/unmarshal with protobuf in addition to json?

If you are good with extending it, my thinking is to set the serialization method in the boltsec constructor so that the Save and Get method implementation would be a bit easier. Maybe even have the caller pass a marshal and unmarshal callback so boltsec itself doesn't have any extra dependencies?

Please let me know what you think of this and if you might accept a PR for it.

func TestDBMCreate(t *testing.T)
func TestDBMCreate(t *testing.T) {
	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)
	}

	return
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.