-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathveil.go
125 lines (105 loc) · 3.19 KB
/
veil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Package veil provides a simple key-value storage mechanism with encryption for sensitive data.
package veil
import (
"encoding/json"
"errors"
"github.com/amalmadhu06/veil/cipher"
"io"
"os"
"sync"
)
// Vile represents a key-value store with encryption capabilities.
type Vile struct {
encodingKey string // The key used for encryption/decryption.
filepath string // The file path to store the data.
mutex sync.Mutex // Mutex to synchronize access to the key-value store.
keyValues map[string]string // The actual key-value data.
}
// NewVile creates a new instance of Vile with the provided encoding key and file path.
func NewVile(encodingKey, filepath string) *Vile {
return &Vile{
encodingKey: encodingKey,
filepath: filepath,
}
}
// Set sets the value for the given key in the key-value store.
// It encrypts the data and saves it to the file.
func (v *Vile) Set(key, value string) error {
v.mutex.Lock()
defer v.mutex.Unlock()
// Load existing data from file, if any.
err := v.load()
if err != nil {
return err
}
// Update the value for the given key.
v.keyValues[key] = value
// Save the updated key-value store to the file.
err = v.save()
return err
}
// Get retrieves the value for the given key from the key-value store.
// It decrypts the data from the file and returns the value.
func (v *Vile) Get(key string) (string, error) {
v.mutex.Lock()
defer v.mutex.Unlock()
// Load data from file.
err := v.load()
if err != nil {
return "", err
}
// Retrieve the value for the given key.
value, ok := v.keyValues[key]
if !ok {
return "", errors.New("secret: no value for that key")
}
return value, nil
}
// save encrypts and saves the key-value store to the file.
func (v *Vile) save() error {
// Open the file for writing.
f, err := os.OpenFile(v.filepath, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer f.Close()
// Create an encrypted writer.
w, err := cipher.EncryptWriter(v.encodingKey, f)
if err != nil {
return err
}
// Write the encrypted key-value store to the writer.
return v.writeKeyValues(w)
}
// load reads and decrypts the key-value store from the file.
func (v *Vile) load() error {
// Open the file for reading.
f, err := os.Open(v.filepath)
if err != nil {
// If the file doesn't exist, create an empty key-value store.
v.keyValues = make(map[string]string)
return nil
}
defer f.Close()
// Create a decrypted reader.
r, err := cipher.DecryptReader(v.encodingKey, f)
if err != nil {
return err
}
// Read the decrypted key-value store from the reader.
return v.readKeyValues(r)
}
// writeKeyValues writes the key-value store to the given writer.
func (v *Vile) writeKeyValues(w io.Writer) error {
// Create a JSON encoder for writing the key-value store.
enc := json.NewEncoder(w)
// Encode the key-value store to JSON and write it to the writer.
return enc.Encode(v.keyValues)
}
// readKeyValues reads the key-value store from the given reader.
func (v *Vile) readKeyValues(r io.Reader) error {
// Create a JSON decoder for reading the key-value store.
dec := json.NewDecoder(r)
// Decode the JSON data and store it in the key-value store.
return dec.Decode(&v.keyValues)
}