61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package go_encryption
|
|
|
|
import "sync"
|
|
|
|
var (
|
|
mutex = sync.RWMutex{}
|
|
std = NewAES128EncryptionProvider()
|
|
)
|
|
|
|
func SetProvider(provider IEncryptionProvider) {
|
|
if provider == nil {
|
|
panic("undefined provider")
|
|
}
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
std = provider
|
|
}
|
|
|
|
// SetEncryptionKey allows to modify the system-wide encryption key.
|
|
func SetEncryptionKey(key []byte) {
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
std.SetEncryptionKey(key)
|
|
}
|
|
|
|
// SetContext allows to modify the system-wide encryption context.
|
|
// N.B.: context must be 12 to 16 bytes in length.
|
|
func SetContext(context []byte) (err error) {
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
return std.SetContext(context)
|
|
}
|
|
|
|
// EncryptData returns the given data as JSON in encrypted form.
|
|
func EncryptData(data interface{}) (encrypted []byte, err error) {
|
|
mutex.RLock()
|
|
defer mutex.RUnlock()
|
|
return std.EncryptData(data)
|
|
}
|
|
|
|
// EncryptToFile saves the given data to the given filepath as JSON in encrypted form.
|
|
func EncryptToFile(data interface{}, filepath string) (err error) {
|
|
mutex.RLock()
|
|
defer mutex.RUnlock()
|
|
return std.EncryptToFile(data, filepath)
|
|
}
|
|
|
|
// DecryptData tries to decrypt the given binary and to unmarshal the resulting JSON to the target interface.
|
|
func DecryptData(binary []byte, target interface{}) (err error) {
|
|
mutex.RLock()
|
|
defer mutex.RUnlock()
|
|
return std.DecryptData(binary, target)
|
|
}
|
|
|
|
// DecryptData tries to decrypt the content of the file specified and to unmarshal the resulting JSON to the target interface.
|
|
func DecryptFile(path string, target interface{}) (err error) {
|
|
mutex.RLock()
|
|
defer mutex.RUnlock()
|
|
return std.DecryptFile(path, target)
|
|
}
|