更改目录结构

This commit is contained in:
bjdgyc
2021-03-01 15:46:08 +08:00
parent 3464d1d10e
commit 0f91c779e3
105 changed files with 29099 additions and 96 deletions

View File

@@ -0,0 +1,40 @@
package utils
import (
"crypto/rand"
"encoding/base64"
mt "math/rand"
"golang.org/x/crypto/bcrypt"
)
func PasswordHash(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
func PasswordVerify(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
// $sha-256$salt-key$hash-abcd
// $sha-512$salt-key$hash-abcd
const (
saltSize = 16
delmiter = "$"
)
func RandSecret(min int, max int) (string, error) {
rb := make([]byte, randInt(min, max))
_, err := rand.Read(rb)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(rb), nil
}
func randInt(min int, max int) int {
return min + mt.Intn(max-min)
}

74
server/pkg/utils/util.go Normal file
View File

@@ -0,0 +1,74 @@
package utils
import (
"fmt"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func InArrStr(arr []string, str string) bool {
for _, d := range arr {
if d == str {
return true
}
}
return false
}
const (
KB = 1024
MB = 1024 * KB
GB = 1024 * MB
TB = 1024 * GB
PB = 1024 * TB
)
func HumanByte(bf interface{}) string {
var hb string
var bAll float64
switch bi := bf.(type) {
case int:
bAll = float64(bi)
case int32:
bAll = float64(bi)
case uint32:
bAll = float64(bi)
case int64:
bAll = float64(bi)
case uint64:
bAll = float64(bi)
case float64:
bAll = float64(bi)
}
switch {
case bAll >= TB:
hb = fmt.Sprintf("%0.2f TB", bAll/TB)
case bAll >= GB:
hb = fmt.Sprintf("%0.2f GB", bAll/GB)
case bAll >= MB:
hb = fmt.Sprintf("%0.2f MB", bAll/MB)
case bAll >= KB:
hb = fmt.Sprintf("%0.2f KB", bAll/KB)
default:
hb = fmt.Sprintf("%0.2f B", bAll)
}
return hb
}
func RandomNum(length int) string {
letterRunes := []rune("abcdefghijklmnpqrstuvwxy1234567890")
bytes := make([]rune, length)
for i := range bytes {
bytes[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(bytes)
}

View File

@@ -0,0 +1,29 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInArrStr(t *testing.T) {
assert := assert.New(t)
arr := []string{"a", "b", "c"}
assert.True(InArrStr(arr, "b"))
assert.False(InArrStr(arr, "d"))
}
func TestHumanByte(t *testing.T) {
assert := assert.New(t)
var s string
s = HumanByte(999)
assert.Equal(s, "999.00 B")
s = HumanByte(10256)
assert.Equal(s, "10.02 KB")
s = HumanByte(99 * 1024 * 1024)
assert.Equal(s, "99.00 MB")
s = HumanByte(1023 * 1024 * 1024)
assert.Equal(s, "1023.00 MB")
s = HumanByte(1024 * 1024 * 1024)
assert.Equal(s, "1.00 GB")
}