mirror of
https://github.com/langgenius/dify-plugin-daemon.git
synced 2026-07-22 17:56:00 -04:00
69cb144fe0
- Updated the .dify_cli.json configuration to include cli_api_url, cli_api_session_id, and cli_api_secret for improved API interaction. - Refactored the callDifyAPI and fetchProviders functions to utilize the new CLI API structure, including HMAC signature generation for enhanced security. - Modified EnvConfig to reflect the new CLI API parameters, ensuring required fields are validated. This update enhances the security and functionality of the CLI by transitioning to a new API structure and improving request handling.
114 lines
2.5 KiB
Go
114 lines
2.5 KiB
Go
package encryption
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/hmac"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"encoding/hex"
|
|
"encoding/pem"
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
func RSASign(rsaPrivateKey *rsa.PrivateKey, data []byte) ([]byte, error) {
|
|
hashed := sha256.Sum256(data)
|
|
return rsa.SignPKCS1v15(rand.Reader, rsaPrivateKey, crypto.SHA256, hashed[:])
|
|
}
|
|
|
|
func VerifySign(rsaPublicKey *rsa.PublicKey, data []byte, sign []byte) error {
|
|
hashed := sha256.Sum256(data)
|
|
return rsa.VerifyPKCS1v15(rsaPublicKey, crypto.SHA256, hashed[:], sign)
|
|
}
|
|
|
|
func AESEncrypt(aesKey []byte, data []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(aesKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
aesGCM, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nonce := make([]byte, aesGCM.NonceSize())
|
|
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cipherText := aesGCM.Seal(nonce, nonce, data, nil)
|
|
return cipherText, nil
|
|
}
|
|
|
|
func AESDecrypt(aesKey []byte, data []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(aesKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
aesGCM, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nonceSize := aesGCM.NonceSize()
|
|
if len(data) < nonceSize {
|
|
return nil, errors.New("ciphertext too short")
|
|
}
|
|
|
|
nonce, cipherText := data[:nonceSize], data[nonceSize:]
|
|
plainText, err := aesGCM.Open(nil, nonce, cipherText, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return plainText, nil
|
|
}
|
|
|
|
func LoadPrivateKey(data []byte) (*rsa.PrivateKey, error) {
|
|
privateKeyBlock, rest := pem.Decode(data)
|
|
if privateKeyBlock == nil || privateKeyBlock.Type != "RSA PRIVATE KEY" {
|
|
return nil, errors.New("failed to decode PEM block containing private key")
|
|
}
|
|
|
|
if len(rest) != 0 {
|
|
return nil, errors.New("extra data included in the PEM block")
|
|
}
|
|
|
|
privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return privateKey, nil
|
|
}
|
|
|
|
func LoadPublicKey(data []byte) (*rsa.PublicKey, error) {
|
|
publicKeyBlock, rest := pem.Decode(data)
|
|
if publicKeyBlock == nil || publicKeyBlock.Type != "RSA PUBLIC KEY" {
|
|
return nil, errors.New("failed to decode PEM block containing public key")
|
|
}
|
|
|
|
if len(rest) != 0 {
|
|
return nil, errors.New("extra data included in the PEM block")
|
|
}
|
|
|
|
publicKey, err := x509.ParsePKCS1PublicKey(publicKeyBlock.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return publicKey, nil
|
|
}
|
|
|
|
func HmacSha256(secret string, data []byte) string {
|
|
h := hmac.New(sha256.New, []byte(secret))
|
|
h.Write(data)
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|