将节点分为普通节点和控制节点
This commit is contained in:
+67
-2
@@ -1,9 +1,14 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
_ "embed"
|
||||
"encoding/base64"
|
||||
"encoding/pem"
|
||||
"github.com/farmerx/gorsa"
|
||||
"log"
|
||||
)
|
||||
|
||||
@@ -11,14 +16,14 @@ import (
|
||||
var rsaCert []byte
|
||||
|
||||
//go:embed server.key
|
||||
var PublicKey []byte
|
||||
var PrivateKey []byte
|
||||
|
||||
var Tlsconfig *tls.Config
|
||||
|
||||
func init() {
|
||||
//内置证书
|
||||
|
||||
cert, err := tls.X509KeyPair(rsaCert, PublicKey)
|
||||
cert, err := tls.X509KeyPair(rsaCert, PrivateKey)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
return
|
||||
@@ -51,3 +56,63 @@ func init() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//go:embed public.pem
|
||||
var publicKey []byte
|
||||
|
||||
|
||||
var privateKey []byte
|
||||
|
||||
// 使用公钥进行加密
|
||||
func RSAEncrypter(msg []byte) []byte {
|
||||
block, _ := pem.Decode(publicKey)
|
||||
pub, _ := x509.ParsePKIXPublicKey(block.Bytes)
|
||||
cipherText, _ := rsa.EncryptPKCS1v15(rand.Reader, pub.(*rsa.PublicKey), msg)
|
||||
return cipherText
|
||||
}
|
||||
func RSAEncrypterStr(msg string) string {
|
||||
block, _ := pem.Decode(publicKey)
|
||||
pub, _ := x509.ParsePKIXPublicKey(block.Bytes)
|
||||
cipherText, _ := rsa.EncryptPKCS1v15(rand.Reader, pub.(*rsa.PublicKey), []byte(msg))
|
||||
return base64.StdEncoding.EncodeToString(cipherText)
|
||||
}
|
||||
|
||||
// 使用私钥进行解密
|
||||
func RSADecrypter(cipherText []byte) []byte {
|
||||
if block, _ := pem.Decode(privateKey); block != nil {
|
||||
p, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
afterDecrypter, _ := rsa.DecryptPKCS1v15(rand.Reader, p, cipherText)
|
||||
return afterDecrypter
|
||||
}
|
||||
return []byte{}
|
||||
}
|
||||
func RSADecrypterStr(cipherText string) string {
|
||||
if block, _ := pem.Decode(privateKey); block != nil {
|
||||
p, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
b, _ := base64.StdEncoding.DecodeString(cipherText)
|
||||
afterDecrypter, _ := rsa.DecryptPKCS1v15(rand.Reader, p, b)
|
||||
return string(afterDecrypter)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func RSAEncrypterByPriv(msg string) string {
|
||||
prienctypt, _ := gorsa.RSA.PriKeyENCTYPT([]byte(msg))
|
||||
return base64.StdEncoding.EncodeToString(prienctypt)
|
||||
}
|
||||
func RSAEncrypterByPrivByte(msg []byte) []byte {
|
||||
prienctypt, _ := gorsa.RSA.PriKeyENCTYPT([]byte(msg))
|
||||
return prienctypt
|
||||
}
|
||||
func RSADecrypterByPub(cipherText string) string{
|
||||
b, _ := base64.StdEncoding.DecodeString(cipherText)
|
||||
pubdecrypt, _ := gorsa.RSA.PubKeyDECRYPT(b)
|
||||
return string(pubdecrypt)
|
||||
}
|
||||
func RSADecrypterByPubByte(cipherText []byte) []byte {
|
||||
pubdecrypt, _ := gorsa.RSA.PubKeyDECRYPT(cipherText)
|
||||
return pubdecrypt
|
||||
}
|
||||
func init(){
|
||||
gorsa.RSA.SetPublicKey(string(publicKey))
|
||||
gorsa.RSA.SetPrivateKey(string(privateKey))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user