Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package avatar
  4. import (
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "strconv"
  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. }