verifier.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package verifier
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "path"
  7. "strconv"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/license/public_key"
  9. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/encryption"
  11. )
  12. // VerifyPlugin is a function that verifies the signature of a plugin
  13. // It takes a plugin decoder and verifies the signature
  14. func VerifyPlugin(decoder decoder.PluginDecoder) error {
  15. // load public key
  16. public_key, err := encryption.LoadPublicKey(public_key.PUBLIC_KEY)
  17. if err != nil {
  18. return err
  19. }
  20. data := new(bytes.Buffer)
  21. // read one by one
  22. err = decoder.Walk(func(filename, dir string) error {
  23. // read file bytes
  24. file, err := decoder.ReadFile(path.Join(dir, filename))
  25. if err != nil {
  26. return err
  27. }
  28. hash := sha256.New()
  29. hash.Write(file)
  30. // write the hash into data
  31. data.Write(hash.Sum(nil))
  32. return nil
  33. })
  34. if err != nil {
  35. return err
  36. }
  37. // get the signature
  38. signature, err := decoder.Signature()
  39. if err != nil {
  40. return err
  41. }
  42. // get the time
  43. created_at, err := decoder.CreateTime()
  44. if err != nil {
  45. return err
  46. }
  47. // write the time into data
  48. data.Write([]byte(strconv.FormatInt(created_at, 10)))
  49. sig_bytes, err := base64.StdEncoding.DecodeString(signature)
  50. if err != nil {
  51. return err
  52. }
  53. // verify signature
  54. err = encryption.VerifySign(public_key, data.Bytes(), sig_bytes)
  55. return err
  56. }