first commit
This commit is contained in:
71
util/crypto.go
Normal file
71
util/crypto.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/go-think/openssl"
|
||||
"io"
|
||||
)
|
||||
|
||||
func AesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
|
||||
data, err := openssl.AesCBCEncrypt(src, key, iv, padding)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(hex.EncodeToString(data)), nil
|
||||
}
|
||||
|
||||
func AesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
|
||||
data, err := hex.DecodeString(string(src))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return openssl.AesCBCDecrypt(data, key, iv, padding)
|
||||
|
||||
}
|
||||
func Md5(text string) string {
|
||||
hashMd5 := md5.New()
|
||||
io.WriteString(hashMd5, text)
|
||||
return fmt.Sprintf("%x", hashMd5.Sum(nil))
|
||||
}
|
||||
|
||||
func Zip(data []byte) ([]byte, error) {
|
||||
|
||||
var b bytes.Buffer
|
||||
gz, _ := gzip.NewWriterLevel(&b, 9)
|
||||
if _, err := gz.Write([]byte(data)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := gz.Flush(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := gz.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func UnZip(data []byte) ([]byte, error) {
|
||||
b := new(bytes.Buffer)
|
||||
binary.Write(b, binary.LittleEndian, data)
|
||||
r, err := gzip.NewReader(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
unzipData, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return unzipData, nil
|
||||
}
|
||||
|
||||
func Password(pwd string, pwdCode string) string {
|
||||
return Md5(pwd + pwdCode)
|
||||
}
|
||||
24
util/math.go
Normal file
24
util/math.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package util
|
||||
|
||||
func MinInt(x, y int) int{
|
||||
if x>y {
|
||||
return y
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func MaxInt(x, y int) int{
|
||||
if x<y {
|
||||
return y
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func AbsInt(x int) int{
|
||||
if x > 0{
|
||||
return x
|
||||
}else {
|
||||
return -x
|
||||
}
|
||||
}
|
||||
|
||||
21
util/msgpack.go
Normal file
21
util/msgpack.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func Unmarshal(data []byte, v interface{}) error {
|
||||
//dec := msgpack.NewDecoder(bytes.NewReader(data))
|
||||
//dec.UseJSONTag(true)
|
||||
//return dec.Decode(v)
|
||||
return json.Unmarshal(data, v)
|
||||
}
|
||||
|
||||
func Marshal(v interface{}) ([]byte, error) {
|
||||
//var buf bytes.Buffer
|
||||
//enc := msgpack.NewEncoder(&buf)
|
||||
//enc.UseJSONTag(true)
|
||||
//err := enc.Encode(v)
|
||||
//return buf.Bytes(), err
|
||||
return json.Marshal(v)
|
||||
}
|
||||
13
util/random.go
Normal file
13
util/random.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package util
|
||||
|
||||
import "math/rand"
|
||||
|
||||
var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
func RandSeq(n int) string {
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
65
util/session.go
Normal file
65
util/session.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-think/openssl"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
//有效时间30天
|
||||
const validTime = 30*24*time.Hour
|
||||
const key = ("1234567890123456")
|
||||
|
||||
type Session struct {
|
||||
MTime time.Time
|
||||
Id int
|
||||
}
|
||||
|
||||
func NewSession(id int, time time.Time) *Session {
|
||||
return &Session{Id: id, MTime: time}
|
||||
}
|
||||
|
||||
func ParseSession(session string) (*Session, error) {
|
||||
if session == ""{
|
||||
return nil, errors.New("session is empty")
|
||||
}
|
||||
decode, err := base64.StdEncoding.DecodeString(session)
|
||||
if err != nil{
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, _ := AesCBCDecrypt(decode, []byte(key), []byte(key),openssl.ZEROS_PADDING)
|
||||
arr := strings.Split(string(data), "|")
|
||||
if len(arr) != 2 {
|
||||
return nil, errors.New("session format error")
|
||||
}
|
||||
|
||||
int, err := strconv.Atoi(arr[0])
|
||||
if err != nil{
|
||||
return nil, err
|
||||
}
|
||||
|
||||
time, err :=time.Parse("2006-01-02 15:04:05", arr[1])
|
||||
if err != nil{
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Session{Id: int, MTime: time}, nil
|
||||
}
|
||||
|
||||
func (self *Session) String() string {
|
||||
timeStr := self.MTime.Format("2006-01-02 15:04:05")
|
||||
str := fmt.Sprintf("%d|%s", self.Id, timeStr)
|
||||
data, _ := AesCBCEncrypt([]byte(str), []byte(key), []byte(key),openssl.ZEROS_PADDING)
|
||||
encode := base64.StdEncoding.EncodeToString(data)
|
||||
return encode
|
||||
}
|
||||
|
||||
func (self *Session) IsValid() bool {
|
||||
diff := time.Now().Sub(self.MTime)
|
||||
return diff - time.Duration(validTime) < 0
|
||||
}
|
||||
Reference in New Issue
Block a user