gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package base
  4. import (
  5. "crypto/hmac"
  6. "crypto/sha1"
  7. "crypto/sha256"
  8. "crypto/subtle"
  9. "encoding/hex"
  10. "fmt"
  11. "hash"
  12. "strconv"
  13. "time"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/util"
  16. "github.com/dustin/go-humanize"
  17. )
  18. // EncodeSha256 string to sha256 hex value.
  19. func EncodeSha256(str string) string {
  20. h := sha256.New()
  21. _, _ = h.Write([]byte(str))
  22. return hex.EncodeToString(h.Sum(nil))
  23. }
  24. // ShortSha is basically just truncating.
  25. // It is DEPRECATED and will be removed in the future.
  26. func ShortSha(sha1 string) string {
  27. return util.TruncateRunes(sha1, 10)
  28. }
  29. // VerifyTimeLimitCode verify time limit code
  30. func VerifyTimeLimitCode(now time.Time, data string, minutes int, code string) bool {
  31. if len(code) <= 18 {
  32. return false
  33. }
  34. startTimeStr := code[:12]
  35. aliveTimeStr := code[12:18]
  36. aliveTime, _ := strconv.Atoi(aliveTimeStr) // no need to check err, if anything wrong, the following code check will fail soon
  37. // check code
  38. retCode := CreateTimeLimitCode(data, aliveTime, startTimeStr, nil)
  39. if subtle.ConstantTimeCompare([]byte(retCode), []byte(code)) != 1 {
  40. return false
  41. }
  42. // check time is expired or not: startTime <= now && now < startTime + minutes
  43. startTime, _ := time.ParseInLocation("200601021504", startTimeStr, time.Local)
  44. return (startTime.Before(now) || startTime.Equal(now)) && now.Before(startTime.Add(time.Minute*time.Duration(minutes)))
  45. }
  46. // TimeLimitCodeLength default value for time limit code
  47. const TimeLimitCodeLength = 12 + 6 + 40
  48. // CreateTimeLimitCode create a time-limited code.
  49. // Format: 12 length date time string + 6 minutes string (not used) + 40 hash string, some other code depends on this fixed length
  50. // If h is nil, then use the default hmac hash.
  51. func CreateTimeLimitCode[T time.Time | string](data string, minutes int, startTimeGeneric T, h hash.Hash) string {
  52. const format = "200601021504"
  53. var start time.Time
  54. var startTimeAny any = startTimeGeneric
  55. if t, ok := startTimeAny.(time.Time); ok {
  56. start = t
  57. } else {
  58. var err error
  59. start, err = time.ParseInLocation(format, startTimeAny.(string), time.Local)
  60. if err != nil {
  61. return "" // return an invalid code because the "parse" failed
  62. }
  63. }
  64. startStr := start.Format(format)
  65. end := start.Add(time.Minute * time.Duration(minutes))
  66. if h == nil {
  67. h = hmac.New(sha1.New, setting.GetGeneralTokenSigningSecret())
  68. }
  69. _, _ = fmt.Fprintf(h, "%s%s%s%s%d", data, hex.EncodeToString(setting.GetGeneralTokenSigningSecret()), startStr, end.Format(format), minutes)
  70. encoded := hex.EncodeToString(h.Sum(nil))
  71. code := fmt.Sprintf("%s%06d%s", startStr, minutes, encoded)
  72. if len(code) != TimeLimitCodeLength {
  73. panic("there is a hard requirement for the length of time-limited code") // it shouldn't happen
  74. }
  75. return code
  76. }
  77. // FileSize calculates the file size and generate user-friendly string.
  78. func FileSize(s int64) string {
  79. return humanize.IBytes(uint64(s))
  80. }
  81. // StringsToInt64s converts a slice of string to a slice of int64.
  82. func StringsToInt64s(strs []string) ([]int64, error) {
  83. if strs == nil {
  84. return nil, nil
  85. }
  86. ints := make([]int64, 0, len(strs))
  87. for _, s := range strs {
  88. if s == "" {
  89. continue
  90. }
  91. n, err := strconv.ParseInt(s, 10, 64)
  92. if err != nil {
  93. return nil, err
  94. }
  95. ints = append(ints, n)
  96. }
  97. return ints, nil
  98. }
  99. // Int64sToStrings converts a slice of int64 to a slice of string.
  100. func Int64sToStrings(ints []int64) []string {
  101. strs := make([]string, len(ints))
  102. for i := range ints {
  103. strs[i] = strconv.FormatInt(ints[i], 10)
  104. }
  105. return strs
  106. }