etag.go 731 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "context"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "encoding/json"
  7. "hash"
  8. "sync"
  9. )
  10. type eTagCalc struct {
  11. hash hash.Hash
  12. enc *json.Encoder
  13. }
  14. var eTagCalcPool = sync.Pool{
  15. New: func() interface{} {
  16. h := sha256.New()
  17. enc := json.NewEncoder(h)
  18. enc.SetEscapeHTML(false)
  19. enc.SetIndent("", "")
  20. return &eTagCalc{h, enc}
  21. },
  22. }
  23. func calcETag(ctx context.Context) string {
  24. c := eTagCalcPool.Get().(*eTagCalc)
  25. defer eTagCalcPool.Put(c)
  26. c.hash.Reset()
  27. c.hash.Write(getImageData(ctx).Data)
  28. footprint := c.hash.Sum(nil)
  29. c.hash.Reset()
  30. c.hash.Write(footprint)
  31. c.hash.Write([]byte(version))
  32. c.enc.Encode(conf)
  33. c.enc.Encode(getProcessingOptions(ctx))
  34. return hex.EncodeToString(c.hash.Sum(nil))
  35. }