As of https://github.com/YumeMichi/elichika/commit/81a78e964dcdb8bb2d123237f966f95e7fba190e Signed-off-by: Yuan Si <do4suki@gmail.com>
62 lines
943 B
Go
62 lines
943 B
Go
// Copyright (C) 2021-2023 YumeMichi
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package utils
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"math/rand"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
rwMutex sync.RWMutex
|
|
)
|
|
|
|
func PathExists(path string) bool {
|
|
_, err := os.Stat(path)
|
|
return err == nil || os.IsExist(err)
|
|
}
|
|
|
|
func ReadAllText(path string) string {
|
|
rwMutex.RLock()
|
|
defer rwMutex.RUnlock()
|
|
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func WriteAllText(path, text string) {
|
|
rwMutex.Lock()
|
|
defer rwMutex.Unlock()
|
|
|
|
_ = os.WriteFile(path, []byte(text), 0644)
|
|
}
|
|
|
|
func SliceXor(s1, s2 []byte) (res []byte) {
|
|
for k, b := range s1 {
|
|
newBt := b ^ s2[k]
|
|
res = append(res, newBt)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func Sub16(str []byte) []byte {
|
|
return str[16:]
|
|
}
|
|
|
|
func RandomStr(len int) string {
|
|
rand.Seed(time.Now().UnixNano())
|
|
mRand := make([]byte, len)
|
|
rand.Read(mRand)
|
|
mRandStr := hex.EncodeToString(mRand)[0:len]
|
|
|
|
return mRandStr
|
|
}
|