mirror of https://github.com/veypi/OneAuth.git
feat: change to vigo
parent
3b9cbe1c1b
commit
959e390126
@ -1,25 +0,0 @@
|
||||
//
|
||||
// libs.go
|
||||
// Copyright (C) 2024 veypi <i@veypi.com>
|
||||
// 2024-11-04 21:50
|
||||
// Distributed under terms of the GPL license.
|
||||
//
|
||||
|
||||
package libs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/veypi/OneBD/rest"
|
||||
)
|
||||
|
||||
func CorsAllowAny(x *rest.X) {
|
||||
origin := x.Request.Header.Get("Origin")
|
||||
x.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
x.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
x.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH, PROPFIND")
|
||||
x.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, depth")
|
||||
if x.Request.Method == http.MethodOptions && x.Request.Header.Get("Access-Control-Request-Method") != "" {
|
||||
x.Stop()
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,173 @@
|
||||
package utils
|
||||
|
||||
//
|
||||
// crypto.go
|
||||
// Copyright (C) 2020 light <light@1870499383@qq.com>
|
||||
//
|
||||
// Distributed under terms of the MIT license.
|
||||
//
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// PKCS7Padding 添加 PKCS#7 填充
|
||||
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(ciphertext)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(ciphertext, padtext...)
|
||||
}
|
||||
|
||||
// PKCS7UnPadding 移除 PKCS#7 填充
|
||||
func PKCS7UnPadding(origData []byte) ([]byte, bool) {
|
||||
length := len(origData)
|
||||
unpadding := int(origData[length-1])
|
||||
if unpadding >= length {
|
||||
return nil, false
|
||||
}
|
||||
return origData[:(length - unpadding)], true
|
||||
}
|
||||
|
||||
// AesEncrypt 使用 AES-256-CBC 进行加密
|
||||
// key 256 bit / 32 Byte
|
||||
// iv 128 bit / 16 Byte
|
||||
func AesEncrypt(plaintext, key, iv []byte) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
plaintext = PKCS7Padding(plaintext, block.BlockSize())
|
||||
blockMode := cipher.NewCBCEncrypter(block, iv)
|
||||
crypted := make([]byte, len(plaintext))
|
||||
blockMode.CryptBlocks(crypted, plaintext)
|
||||
|
||||
return base64.StdEncoding.EncodeToString(crypted), nil
|
||||
}
|
||||
|
||||
// AesDecrypt 使用 AES-256-CBC 进行解密
|
||||
func AesDecrypt(encrypted, key, iv []byte) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
encrypted, err = base64.StdEncoding.DecodeString(string(encrypted))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
blockMode := cipher.NewCBCDecrypter(block, iv)
|
||||
origData := make([]byte, len(encrypted))
|
||||
blockMode.CryptBlocks(origData, encrypted)
|
||||
origData, ok := PKCS7UnPadding(origData)
|
||||
if !ok {
|
||||
return "", errors.New("PKCS7UnPadding error")
|
||||
}
|
||||
return string(origData), nil
|
||||
}
|
||||
|
||||
// rsa
|
||||
|
||||
func GetRsaKey(bits int) (public *rsa.PublicKey, private *rsa.PrivateKey, err error) {
|
||||
private, err = rsa.GenerateKey(rand.Reader, bits)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
public = &private.PublicKey
|
||||
return
|
||||
}
|
||||
|
||||
func GetPublicStr(key *rsa.PublicKey) (string, error) {
|
||||
der, err := x509.MarshalPKIXPublicKey(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
publicBlock := &pem.Block{
|
||||
Type: "PUBLIC KEY",
|
||||
Bytes: der,
|
||||
}
|
||||
return string(pem.EncodeToMemory(publicBlock)), nil
|
||||
}
|
||||
|
||||
func GetPrivateStr(key *rsa.PrivateKey) (string, error) {
|
||||
derStream := x509.MarshalPKCS1PrivateKey(key)
|
||||
priBlock := &pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: derStream,
|
||||
}
|
||||
return string(pem.EncodeToMemory(priBlock)), nil
|
||||
}
|
||||
|
||||
func GetPublicFromStr(key string) (*rsa.PublicKey, error) {
|
||||
//解密pem格式的公钥
|
||||
block, _ := pem.Decode([]byte(key))
|
||||
if block == nil {
|
||||
return nil, errors.New("public key error")
|
||||
}
|
||||
// 解析公钥
|
||||
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 类型断言
|
||||
pub := pubInterface.(*rsa.PublicKey)
|
||||
return pub, nil
|
||||
}
|
||||
|
||||
func GetPrivateFromStr(key string) (*rsa.PrivateKey, error) {
|
||||
block, _ := pem.Decode([]byte(key))
|
||||
if block == nil {
|
||||
return nil, errors.New("private key error")
|
||||
}
|
||||
//解析PKCS1格式的私钥
|
||||
return x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
}
|
||||
|
||||
func RsaEncode(msg string, key *rsa.PublicKey) (string, error) {
|
||||
encryptedBytes, err := rsa.EncryptOAEP(
|
||||
sha256.New(),
|
||||
rand.Reader,
|
||||
key,
|
||||
[]byte(msg),
|
||||
nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ToBase64(encryptedBytes), nil
|
||||
}
|
||||
|
||||
func RsaDecode(msg string, key *rsa.PrivateKey) (string, error) {
|
||||
raw, err := FromBase64(msg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
decryptedBytes, err := key.Decrypt(nil, raw, &rsa.OAEPOptions{Hash: crypto.SHA256})
|
||||
return string(decryptedBytes), err
|
||||
}
|
||||
|
||||
func RsaSign(msg string, key *rsa.PrivateKey) (string, error) {
|
||||
signature, err := rsa.SignPSS(rand.Reader, key, crypto.SHA256, HashSha256Byte([]byte(msg)), nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ToBase64(signature), nil
|
||||
}
|
||||
|
||||
func RsaCheckSign(msg string, sign string, key *rsa.PublicKey) error {
|
||||
raw, err := FromBase64(sign)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rsa.VerifyPSS(key, crypto.SHA256, HashSha256Byte([]byte(msg)), raw, nil)
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func HashMd5(s string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(s))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
func HashSha256Byte(msg []byte) []byte {
|
||||
msgHash := sha256.New()
|
||||
_, err := msgHash.Write(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
msgHashSum := msgHash.Sum(nil)
|
||||
return msgHashSum
|
||||
}
|
||||
|
||||
func HashSha256(msg string) string {
|
||||
return hex.EncodeToString(HashSha256Byte([]byte(msg)))
|
||||
}
|
||||
|
||||
func ToBase64(src []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(src)
|
||||
}
|
||||
|
||||
func FromBase64(src string) ([]byte, error) {
|
||||
return base64.StdEncoding.DecodeString(src)
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
//
|
||||
// utils.go
|
||||
// Copyright (C) 2025 veypi <i@veypi.com>
|
||||
// 2025-07-15 17:13
|
||||
// Distributed under terms of the MIT license.
|
||||
//
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
|
||||
var size = int32(len(letters))
|
||||
var seed = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
// RandSeq produce random string seq
|
||||
func RandSeq(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letters[seed.Int31n(size)]
|
||||
}
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
func Rand(n int) []byte {
|
||||
b := make([]byte, n)
|
||||
seed.Read(b)
|
||||
return b
|
||||
}
|
||||
Loading…
Reference in New Issue