verifier.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package decoder
  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/utils/encryption"
  10. )
  11. // VerifyPlugin is a function that verifies the signature of a plugin
  12. // It takes a plugin decoder and verifies the signature
  13. func VerifyPlugin(decoder PluginDecoder) error {
  14. // load public key
  15. publicKey, err := encryption.LoadPublicKey(public_key.PUBLIC_KEY)
  16. if err != nil {
  17. return err
  18. }
  19. data := new(bytes.Buffer)
  20. // read one by one
  21. err = decoder.Walk(func(filename, dir string) error {
  22. // read file bytes
  23. file, err := decoder.ReadFile(path.Join(dir, filename))
  24. if err != nil {
  25. return err
  26. }
  27. hash := sha256.New()
  28. hash.Write(file)
  29. // write the hash into data
  30. data.Write(hash.Sum(nil))
  31. return nil
  32. })
  33. if err != nil {
  34. return err
  35. }
  36. // get the signature
  37. signature, err := decoder.Signature()
  38. if err != nil {
  39. return err
  40. }
  41. // get the time
  42. createdAt, err := decoder.CreateTime()
  43. if err != nil {
  44. return err
  45. }
  46. // write the time into data
  47. data.Write([]byte(strconv.FormatInt(createdAt, 10)))
  48. sigBytes, err := base64.StdEncoding.DecodeString(signature)
  49. if err != nil {
  50. return err
  51. }
  52. // verify signature
  53. err = encryption.VerifySign(publicKey, data.Bytes(), sigBytes)
  54. return err
  55. }