Merge branch 'main' of https://git.apinb.com/bsm-sdk/core
This commit is contained in:
commit
ffb706df32
20
README.md
20
README.md
|
@ -5,3 +5,23 @@ go env -w GONOPROXY=git.apinb.com/*
|
|||
go env -w GOINSECURE=git.apinb.com/*
|
||||
go env -w GONOSUMDB=git.apinb.com/*
|
||||
```
|
||||
# crypto 加密与解密
|
||||
## GCM加密
|
||||
```
|
||||
AESGCMEncrypt GCM 加密
|
||||
AESGCMDecrypt GCM 解密
|
||||
```
|
||||
## CBC加密
|
||||
```
|
||||
Encrypt CBC加密
|
||||
Decrypt CBC解密
|
||||
```
|
||||
## ECB加密
|
||||
```
|
||||
AesEncryptECB ECB加密
|
||||
AesDecryptECB ECB解密
|
||||
```
|
||||
## 环境变量检测
|
||||
```
|
||||
AesKeyCheck 秘钥环境变量检测
|
||||
```
|
||||
|
|
|
@ -4,10 +4,58 @@ import (
|
|||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// AES加密
|
||||
// =================== GCM ======================
|
||||
// AEC GCM 加密
|
||||
func AESGCMEncrypt(plaintext, key []byte) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
ciphertext := gcm.Seal(nonce, nonce, plaintext, nil)
|
||||
return hex.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
// AEC GCM 解密
|
||||
func AESGCMDecrypt(ciphertext string, key []byte) ([]byte, error) {
|
||||
data, err := hex.DecodeString(ciphertext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nonceSize := gcm.NonceSize()
|
||||
if len(data) < nonceSize {
|
||||
return nil, errors.New("密文无效")
|
||||
}
|
||||
nonce, cipherbyte := data[:nonceSize], data[nonceSize:]
|
||||
return gcm.Open(nil, nonce, cipherbyte, nil)
|
||||
}
|
||||
|
||||
// =================== CBC ======================
|
||||
// AES CBC加密
|
||||
func Encrypt(key string, iv string, data string) string {
|
||||
if len(data) == 0 {
|
||||
return ""
|
||||
|
@ -24,7 +72,7 @@ func Encrypt(key string, iv string, data string) string {
|
|||
return data
|
||||
}
|
||||
|
||||
// AES解密
|
||||
// AES CBC解密
|
||||
func Decrypt(key string, iv string, data string) string {
|
||||
if len(data) == 0 {
|
||||
return ""
|
||||
|
@ -102,3 +150,24 @@ func generateKey(key []byte) (genKey []byte) {
|
|||
}
|
||||
return genKey
|
||||
}
|
||||
|
||||
func AesKeyCheck(key string) (string, error) {
|
||||
// 从环境变量获取密钥
|
||||
keyHex := os.Getenv(key)
|
||||
if keyHex == "" {
|
||||
fmt.Println("环境变量 RST_KEY 未设置")
|
||||
return "", errors.New("环境变量 RST_KEY 未设置")
|
||||
}
|
||||
// 解码十六进制字符串的密钥
|
||||
byteKey, err := hex.DecodeString(keyHex)
|
||||
if err != nil {
|
||||
fmt.Printf("密钥解码失败: %v\n", err)
|
||||
return "", errors.New("密钥解码失败")
|
||||
}
|
||||
// 检查密钥长度
|
||||
if len(byteKey) != 16 && len(key) != 24 && len(key) != 32 {
|
||||
fmt.Printf("无效的密钥长度: %d 字节 (需要16,24或32字节)\n", len(key))
|
||||
return "", errors.New("无效的密钥长度,需要16,24或32字节")
|
||||
}
|
||||
return keyHex, nil
|
||||
}
|
||||
|
|
|
@ -69,13 +69,13 @@ func init() {
|
|||
}
|
||||
|
||||
func WatchCheckLicence(licPath, licName string) {
|
||||
for {
|
||||
utils.SetInterval(func() {
|
||||
if CheckLicence(licPath, licName) == false {
|
||||
log.Println("授权文件失效,请重新部署授权文件:", licPath)
|
||||
os.Exit(99)
|
||||
}
|
||||
time.Sleep(time.Hour * 1)
|
||||
}
|
||||
|
||||
}, time.Hour*1)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
|
|
@ -5,6 +5,10 @@ type LogItem struct {
|
|||
OpName string `json:"op_name"`
|
||||
OpType string `json:"op_type"`
|
||||
Text string `json:"text"`
|
||||
Code string `json:"code"`
|
||||
Level uint `json:"level"`
|
||||
Ip string `json:"ip"`
|
||||
Module string `json:"module"`
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
36
types/db.go
36
types/db.go
|
@ -10,42 +10,42 @@ type (
|
|||
|
||||
// sql options
|
||||
SqlOptions struct {
|
||||
MaxIdleConns int
|
||||
MaxOpenConns int
|
||||
MaxIdleConns int `gorm:"column:max_idle_conns;" json:"max_idle_conns"`
|
||||
MaxOpenConns int `gorm:"column:max_open_conns;" json:"max_open_conns"`
|
||||
ConnMaxLifetime time.Duration
|
||||
|
||||
LogStdout bool
|
||||
Debug bool
|
||||
LogStdout bool `gorm:"column:log_stdout;" json:"log_stdout"`
|
||||
Debug bool `gorm:"column:debug;" json:"debug"`
|
||||
}
|
||||
|
||||
// standard ID,Identity definition.
|
||||
Std_IDIdentity struct {
|
||||
ID uint `gorm:"primarykey;" json:"id"`
|
||||
ID uint `gorm:"column:id;primarykey;" json:"id"`
|
||||
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;" json:"identity"` // 唯一标识,24位NanoID,36位为ULID
|
||||
}
|
||||
|
||||
// standard ID,Created,Updated,Deleted definition.
|
||||
Std_IICUDS struct {
|
||||
ID uint `gorm:"primarykey;" json:"id"`
|
||||
ID uint `gorm:"column:id;primarykey;" json:"id"`
|
||||
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;" json:"identity"` // 唯一标识,24位NanoID,36位为ULID
|
||||
CreatedAt time.Time `gorm:"" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
|
||||
Status int8 `gorm:"default:0;index;" json:"status"` // 状态:默认为0,-1禁止,1为正常
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:TIMESTAMP;" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:TIMESTAMP;" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:TIMESTAMP;index;" json:"deleted_at"`
|
||||
Status int8 `gorm:"column:status;default:0;index;" json:"status"` // 状态:默认为0,-1禁止,1为正常
|
||||
}
|
||||
|
||||
// standard ID,Identity,Created,Updated,Deleted,Status definition.
|
||||
Std_ICUD struct {
|
||||
ID uint `gorm:"primarykey;" json:"id"`
|
||||
CreatedAt time.Time `gorm:"" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
|
||||
ID uint `gorm:"column:id;primarykey;" json:"id"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:TIMESTAMP;" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:TIMESTAMP;index;" json:"deleted_at"`
|
||||
}
|
||||
|
||||
// standard ID,Created definition.
|
||||
Std_IdCreated struct {
|
||||
ID uint `gorm:"primarykey;" json:"id"`
|
||||
CreatedAt time.Time `gorm:"" json:"created_at"`
|
||||
ID uint `gorm:"column:id;primarykey;" json:"id"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:TIMESTAMP;" json:"created_at"`
|
||||
}
|
||||
|
||||
// standard PassportID,PassportIdentity definition.
|
||||
|
@ -62,7 +62,7 @@ type (
|
|||
|
||||
// standard ID definition.
|
||||
Std_ID struct {
|
||||
ID uint `gorm:"primarykey;" json:"id"`
|
||||
ID uint `gorm:"column:id;primarykey;" json:"id"`
|
||||
}
|
||||
|
||||
// standard Identity definition.
|
||||
|
@ -72,6 +72,6 @@ type (
|
|||
|
||||
// standard Status definition.
|
||||
Std_Status struct {
|
||||
Status int64 `gorm:"default:0;index;" json:"status"` // 状态:默认为0,-1禁止,1为正常
|
||||
Status int64 `gorm:"column:status;default:0;index;" json:"status"` // 状态:默认为0,-1禁止,1为正常
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue