package encryptedstring import ( "errors" "os" "path/filepath" "strings" "sync" ) const ( // defaultIdentifier is the prefix for an encrypted representation of an actual string value defaultIdentifier = "$!|" ) var ( encryptionProviderMutex sync.Mutex encryptionProvider IDataCryptor ) // Init initialises the global IDataCryptor instance, if it has not // been set (or used and hence been initalised with a default). func Init(idc IDataCryptor) (err error) { encryptionProviderMutex.Lock() defer encryptionProviderMutex.Unlock() if encryptionProvider != nil { return errors.New("encryptionProvider already defined") } encryptionProvider = idc return } func getEncryptionProvider() IDataCryptor { encryptionProviderMutex.Lock() defer encryptionProviderMutex.Unlock() if encryptionProvider == nil { encryptionProvider = NewDefaultDataCryptor() } return encryptionProvider } func getWorkingDirectory() (workingDirectory string) { workingDirectory = "." fullexecpath, err := os.Executable() if err == nil { workingDirectory, _ = filepath.Split(fullexecpath) workingDirectory = getOSIndependentPath(workingDirectory) } return } // getOSIndependentPath transforms Windows-native paths into Linux-compatible ones. func getOSIndependentPath(path string) string { if len(path) == 0 { return "" } return strings.ReplaceAll(strings.ReplaceAll(path, "\\", "/"), "//", "/") } // getLocalPath returns the path to the local startup directory func getLocalPath() string { local := getWorkingDirectory() if !strings.HasSuffix(local, "/") { local += "/" } return getOSIndependentPath(local) } func unquoteBytes(s []byte) (t []byte, ok bool) { if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { return } return s[1 : len(s)-1], true }