first commit

This commit is contained in:
Yangtao
2025-11-18 17:48:20 +08:00
commit 6e56cab848
196 changed files with 65809 additions and 0 deletions

71
pkg/secure/tool.go Normal file
View File

@ -0,0 +1,71 @@
package secure
import (
"crypto/ecdsa"
"crypto/elliptic"
randcrypto "crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"math/rand"
"time"
)
type PassLevel string
const (
High PassLevel = "advance"
Mid PassLevel = "mix"
Low PassLevel = "char"
Danger PassLevel = "num"
nUmStr = "0123456789"
charStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
specStr = "+=-@#~,.[]()!%^*$"
)
var (
r = rand.New(rand.NewSource(time.Now().UnixNano()))
)
func GeneratePasswd(level PassLevel, length int32) string {
var passwd = make([]byte, length, length)
var sourceStr string
switch level {
case Danger:
sourceStr = nUmStr
case Low:
sourceStr = charStr
case Mid:
sourceStr = fmt.Sprintf("%s%s", nUmStr, charStr)
case High:
sourceStr = fmt.Sprintf("%s%s%s", nUmStr, charStr, specStr)
default:
sourceStr = fmt.Sprintf("%s%s", nUmStr, charStr)
}
for i := range passwd {
index := r.Intn(len(sourceStr))
passwd[i] = sourceStr[index]
}
return string(passwd)
}
func GenerateECDSAKeyPem() (primaryKey, publicKey []byte, e error) {
var (
key *ecdsa.PrivateKey
sec []byte
pk []byte
)
if key, e = ecdsa.GenerateKey(elliptic.P256(), randcrypto.Reader); e != nil {
return
}
if sec, e = x509.MarshalECPrivateKey(key); e != nil {
return
}
if pk, e = x509.MarshalPKIXPublicKey(key.Public()); e != nil {
return
}
primaryKey = pem.EncodeToMemory(&pem.Block{Type: "ECD PRIVATE KEY", Bytes: sec})
publicKey = pem.EncodeToMemory(&pem.Block{Type: "ECD PUBLIC KEY", Bytes: pk})
return
}