bytes.go 2.7 KB

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