crypto.go 783 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package crypto
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/0xJacky/Nginx-UI/internal/crypto"
  6. "github.com/gin-gonic/gin"
  7. "github.com/google/uuid"
  8. "github.com/uozi-tech/cosy"
  9. )
  10. // GetPublicKey generates a new ED25519 key pair and registers it in the cache
  11. func GetPublicKey(c *gin.Context) {
  12. var data struct {
  13. Timestamp int64 `json:"timestamp" binding:"required"`
  14. Fingerprint string `json:"fingerprint" binding:"required"`
  15. }
  16. if !cosy.BindAndValid(c, &data) {
  17. return
  18. }
  19. if time.Now().Unix()-data.Timestamp > 10 {
  20. cosy.ErrHandler(c, crypto.ErrTimeout)
  21. return
  22. }
  23. params, err := crypto.GetCryptoParams()
  24. if err != nil {
  25. cosy.ErrHandler(c, err)
  26. return
  27. }
  28. c.JSON(http.StatusOK, gin.H{
  29. "public_key": params.PublicKey,
  30. "request_id": uuid.NewString(),
  31. })
  32. }