You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hash.go 994B

1234567891011121314151617181920212223242526272829
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package avatar
  4. import (
  5. "encoding/hex"
  6. "strconv"
  7. "github.com/minio/sha256-simd"
  8. )
  9. // HashAvatar will generate a unique string, which ensures that when there's a
  10. // different unique ID while the data is the same, it will generate a different
  11. // output. It will generate the output according to:
  12. // HEX(HASH(uniqueID || - || data))
  13. // The hash being used is SHA256.
  14. // The sole purpose of the unique ID is to generate a distinct hash Such that
  15. // two unique IDs with the same data will have a different hash output.
  16. // The "-" byte is important to ensure that data cannot be modified such that
  17. // the first byte is a number, which could lead to a "collision" with the hash
  18. // of another unique ID.
  19. func HashAvatar(uniqueID int64, data []byte) string {
  20. h := sha256.New()
  21. h.Write([]byte(strconv.FormatInt(uniqueID, 10)))
  22. h.Write([]byte{'-'})
  23. h.Write(data)
  24. return hex.EncodeToString(h.Sum(nil))
  25. }