before merge to main
This commit is contained in:
61
examples/encode/main.go
Normal file
61
examples/encode/main.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Пример использования Encode — шифрование записей в коллекции (AES-256-GCM).
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"linedb/pkg/linedb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
os.RemoveAll("./data")
|
||||
|
||||
initOptions := &linedb.LineDbInitOptions{
|
||||
CacheSize: 100,
|
||||
CacheTTL: time.Minute,
|
||||
DBFolder: "./data",
|
||||
Collections: []linedb.JSONLFileOptions{
|
||||
{
|
||||
CollectionName: "secret_users",
|
||||
AllocSize: 256,
|
||||
Encode: true,
|
||||
EncodeKey: "my-secret-password-12345",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
db := linedb.NewLineDb(nil)
|
||||
if err := db.Init(false, initOptions); err != nil {
|
||||
log.Fatalf("Init failed: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
opts := linedb.LineDbAdapterOptions{}
|
||||
|
||||
// Вставка
|
||||
users := []any{
|
||||
map[string]any{"name": "alice", "email": "alice@secret.com", "role": "admin"},
|
||||
map[string]any{"name": "bob", "email": "bob@secret.com", "role": "user"},
|
||||
}
|
||||
if err := db.Insert(users, "secret_users", opts); err != nil {
|
||||
log.Fatalf("Insert failed: %v", err)
|
||||
}
|
||||
fmt.Println("Insert OK")
|
||||
|
||||
// Чтение
|
||||
all, err := db.Read("secret_users", opts)
|
||||
if err != nil {
|
||||
log.Fatalf("Read failed: %v", err)
|
||||
}
|
||||
fmt.Printf("Records: %d\n", len(all))
|
||||
for _, r := range all {
|
||||
fmt.Printf(" %+v\n", r)
|
||||
}
|
||||
|
||||
// Файл содержит зашифрованные строки (base64)
|
||||
raw, _ := os.ReadFile("./data/secret_users.jsonl")
|
||||
fmt.Printf("\nRaw file (encrypted base64):\n%s\n", string(raw))
|
||||
}
|
||||
Reference in New Issue
Block a user