mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-03 01:41:44 +00:00
master -> develop (#20)
* Squash merge develop into master * feat: add automated production release workflow * feat: add Gitflow workflow for syncing branches * ci: update workflow runner to latest ubuntu version * feat: enable buf.build publishing on master and develop branches --------- Co-authored-by: Prad Nukala <prad@sonr.io>
This commit is contained in:
co-authored by
Prad Nukala
parent
7b0693ef67
commit
228adb7f93
@@ -0,0 +1,54 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
|
||||
"github.com/ncruces/go-sqlite3/gormlite"
|
||||
"golang.org/x/crypto/argon2"
|
||||
"gorm.io/gorm"
|
||||
"lukechampine.com/adiantum/hbsh"
|
||||
"lukechampine.com/adiantum/hpolyc"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
func New(opts ...DBOption) (*DB, error) {
|
||||
config := &DBConfig{
|
||||
fileName: "vault.db",
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(config)
|
||||
}
|
||||
gormdb, err := gorm.Open(gormlite.Open(config.ConnectionString()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db, err := createInitialTables(gormdb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// HBSH creates an HBSH cipher given a key.
|
||||
func (c *DB) HBSH(key []byte) *hbsh.HBSH {
|
||||
if len(key) != 32 {
|
||||
// Key is not appropriate, return nil.
|
||||
return nil
|
||||
}
|
||||
return hpolyc.New(key)
|
||||
}
|
||||
|
||||
// KDF gets a key from a secret.
|
||||
func (c *DB) KDF(secret string) []byte {
|
||||
if secret == "" {
|
||||
// No secret is given, generate a random key.
|
||||
key := make([]byte, 32)
|
||||
n, _ := rand.Read(key)
|
||||
return key[:n]
|
||||
}
|
||||
// Hash the secret with a KDF.
|
||||
return argon2.IDKey([]byte(secret), []byte("hpolyc"), 3, 64*1024, 4, 32)
|
||||
}
|
||||
Reference in New Issue
Block a user