parent
31594c38da
commit
736e605fb7
@ -0,0 +1,3 @@
|
|||||||
|
module gopkg.in/dtapps/go-library.v2/daes
|
||||||
|
|
||||||
|
go 1.16
|
@ -0,0 +1,57 @@
|
|||||||
|
package v20210726
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"encoding/base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Encrypt 加密 aes_128_cbc
|
||||||
|
func Encrypt(encryptStr string, key []byte, iv string) (string, error) {
|
||||||
|
encryptBytes := []byte(encryptStr)
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
blockSize := block.BlockSize()
|
||||||
|
encryptBytes = pkcs5Padding(encryptBytes, blockSize)
|
||||||
|
|
||||||
|
blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
|
||||||
|
encrypted := make([]byte, len(encryptBytes))
|
||||||
|
blockMode.CryptBlocks(encrypted, encryptBytes)
|
||||||
|
return base64.URLEncoding.EncodeToString(encrypted), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt 解密
|
||||||
|
func Decrypt(decryptStr string, key []byte, iv string) (string, error) {
|
||||||
|
decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
blockMode := cipher.NewCBCDecrypter(block, []byte(iv))
|
||||||
|
decrypted := make([]byte, len(decryptBytes))
|
||||||
|
|
||||||
|
blockMode.CryptBlocks(decrypted, decryptBytes)
|
||||||
|
decrypted = pkcs5UnPadding(decrypted)
|
||||||
|
return string(decrypted), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func pkcs5Padding(cipherText []byte, blockSize int) []byte {
|
||||||
|
padding := blockSize - len(cipherText)%blockSize
|
||||||
|
padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||||
|
return append(cipherText, padText...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pkcs5UnPadding(decrypted []byte) []byte {
|
||||||
|
length := len(decrypted)
|
||||||
|
unPadding := int(decrypted[length-1])
|
||||||
|
return decrypted[:(length - unPadding)]
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
module gopkg.in/dtapps/go-library.v2/dtime
|
||||||
|
|
||||||
|
go 1.16
|
@ -0,0 +1,42 @@
|
|||||||
|
package v20210726
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
const (
|
||||||
|
RFC822Format = "Mon, 02 Jan 2006 15:04:05 MST"
|
||||||
|
ISO8601Format = "2006-01-02T15:04:05Z"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NowUTCSeconds() int64 { return time.Now().UTC().Unix() }
|
||||||
|
|
||||||
|
func NowUTCNanoSeconds() int64 { return time.Now().UTC().UnixNano() }
|
||||||
|
|
||||||
|
// GetCurrentDate 获取当前的时间 - 字符串
|
||||||
|
func GetCurrentDate() string {
|
||||||
|
return time.Now().Format("2006/01/02 15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCurrentUnix 获取当前的时间 - Unix时间戳
|
||||||
|
func GetCurrentUnix() int64 {
|
||||||
|
return time.Now().Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCurrentMilliUnix 获取当前的时间 - 毫秒级时间戳
|
||||||
|
func GetCurrentMilliUnix() int64 {
|
||||||
|
return time.Now().UnixNano() / 1000000
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCurrentNanoUnix 获取当前的时间 - 纳秒级时间戳
|
||||||
|
func GetCurrentNanoUnix() int64 {
|
||||||
|
return time.Now().UnixNano()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCurrentWjDate 获取当前的时间 - 字符串 - 没有间隔
|
||||||
|
func GetCurrentWjDate() string {
|
||||||
|
return time.Now().Format("20060102")
|
||||||
|
}
|
||||||
|
|
||||||
|
func FormatISO8601Date(timestampSecond int64) string {
|
||||||
|
tm := time.Unix(timestampSecond, 0).UTC()
|
||||||
|
return tm.Format(ISO8601Format)
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package v20210726
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(GetCurrentDate())
|
||||||
|
fmt.Println(GetCurrentUnix())
|
||||||
|
fmt.Println(GetCurrentMilliUnix())
|
||||||
|
fmt.Println(GetCurrentNanoUnix())
|
||||||
|
fmt.Println(GetCurrentWjDate())
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package v20210726
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestName(t *testing.T) {
|
||||||
|
fmt.Println(GetCurrentDate())
|
||||||
|
fmt.Println(GetCurrentUnix())
|
||||||
|
fmt.Println(GetCurrentMilliUnix())
|
||||||
|
fmt.Println(GetCurrentNanoUnix())
|
||||||
|
fmt.Println(GetCurrentWjDate())
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package time
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(GetCurrentDate())
|
||||||
|
fmt.Println(GetCurrentUnix())
|
||||||
|
fmt.Println(GetCurrentMilliUnix())
|
||||||
|
fmt.Println(GetCurrentNanoUnix())
|
||||||
|
fmt.Println(GetCurrentWjDate())
|
||||||
|
}
|
@ -1,14 +1,14 @@
|
|||||||
package time
|
package time
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestName(t *testing.T) {
|
func TestName(t *testing.T) {
|
||||||
log.Println(GetCurrentDate())
|
fmt.Println(GetCurrentDate())
|
||||||
log.Println(GetCurrentUnix())
|
fmt.Println(GetCurrentUnix())
|
||||||
log.Println(GetCurrentMilliUnix())
|
fmt.Println(GetCurrentMilliUnix())
|
||||||
log.Println(GetCurrentNanoUnix())
|
fmt.Println(GetCurrentNanoUnix())
|
||||||
log.Println(GetCurrentWjDate())
|
fmt.Println(GetCurrentWjDate())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue