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.

150 lines
2.5 KiB

  1. package util
  2. import (
  3. "crypto/md5"
  4. "crypto/rand"
  5. "encoding/base64"
  6. "fmt"
  7. "io"
  8. )
  9. // BytesToHumanReadable returns the converted human readable representation of the bytes.
  10. func BytesToHumanReadable(b uint64) string {
  11. const unit = 1024
  12. if b < unit {
  13. return fmt.Sprintf("%d B", b)
  14. }
  15. div, exp := uint64(unit), 0
  16. for n := b / unit; n >= unit; n /= unit {
  17. div *= unit
  18. exp++
  19. }
  20. return fmt.Sprintf("%.2f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
  21. }
  22. // big endian
  23. func BytesToUint64(b []byte) (v uint64) {
  24. length := uint(len(b))
  25. for i := uint(0); i < length-1; i++ {
  26. v += uint64(b[i])
  27. v <<= 8
  28. }
  29. v += uint64(b[length-1])
  30. return
  31. }
  32. func BytesToUint32(b []byte) (v uint32) {
  33. length := uint(len(b))
  34. for i := uint(0); i < length-1; i++ {
  35. v += uint32(b[i])
  36. v <<= 8
  37. }
  38. v += uint32(b[length-1])
  39. return
  40. }
  41. func BytesToUint16(b []byte) (v uint16) {
  42. v += uint16(b[0])
  43. v <<= 8
  44. v += uint16(b[1])
  45. return
  46. }
  47. func Uint64toBytes(b []byte, v uint64) {
  48. for i := uint(0); i < 8; i++ {
  49. b[7-i] = byte(v >> (i * 8))
  50. }
  51. }
  52. func Uint32toBytes(b []byte, v uint32) {
  53. for i := uint(0); i < 4; i++ {
  54. b[3-i] = byte(v >> (i * 8))
  55. }
  56. }
  57. func Uint16toBytes(b []byte, v uint16) {
  58. b[0] = byte(v >> 8)
  59. b[1] = byte(v)
  60. }
  61. func Uint8toBytes(b []byte, v uint8) {
  62. b[0] = byte(v)
  63. }
  64. // returns a 64 bit big int
  65. func HashStringToLong(dir string) (v int64) {
  66. h := md5.New()
  67. io.WriteString(h, dir)
  68. b := h.Sum(nil)
  69. v += int64(b[0])
  70. v <<= 8
  71. v += int64(b[1])
  72. v <<= 8
  73. v += int64(b[2])
  74. v <<= 8
  75. v += int64(b[3])
  76. v <<= 8
  77. v += int64(b[4])
  78. v <<= 8
  79. v += int64(b[5])
  80. v <<= 8
  81. v += int64(b[6])
  82. v <<= 8
  83. v += int64(b[7])
  84. return
  85. }
  86. func HashToInt32(data []byte) (v int32) {
  87. h := md5.New()
  88. h.Write(data)
  89. b := h.Sum(nil)
  90. v += int32(b[0])
  91. v <<= 8
  92. v += int32(b[1])
  93. v <<= 8
  94. v += int32(b[2])
  95. v <<= 8
  96. v += int32(b[3])
  97. return
  98. }
  99. func Base64Encode(data []byte) string {
  100. return base64.StdEncoding.EncodeToString(data)
  101. }
  102. func Base64Md5(data []byte) string {
  103. return Base64Encode(Md5(data))
  104. }
  105. func Md5(data []byte) []byte {
  106. hash := md5.New()
  107. hash.Write(data)
  108. return hash.Sum(nil)
  109. }
  110. func Md5String(data []byte) string {
  111. return fmt.Sprintf("%x", Md5(data))
  112. }
  113. func Base64Md5ToBytes(contentMd5 string) []byte {
  114. data, err := base64.StdEncoding.DecodeString(contentMd5)
  115. if err != nil {
  116. return nil
  117. }
  118. return data
  119. }
  120. func RandomInt32() int32 {
  121. buf := make([]byte, 4)
  122. rand.Read(buf)
  123. return int32(BytesToUint32(buf))
  124. }
  125. func RandomBytes(byteCount int) []byte {
  126. buf := make([]byte, byteCount)
  127. rand.Read(buf)
  128. return buf
  129. }