block.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package file
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "io"
  6. "math"
  7. "os"
  8. "strconv"
  9. )
  10. // Get info of block
  11. func GetBlockInfo(fileSize int64) (blockSize int, length int) {
  12. switch {
  13. case fileSize <= 1<<28: //256M
  14. blockSize = 1 << 17 //128kb
  15. case fileSize <= 1<<29: //512M
  16. blockSize = 1 << 18 //256kb
  17. case fileSize <= 1<<30: //1G
  18. blockSize = 1 << 19 //512kb
  19. case fileSize <= 1<<31: //2G
  20. blockSize = 1 << 20 //(mb)
  21. case fileSize <= 1<<32: //4G
  22. blockSize = 1 << 21 //2mb
  23. case fileSize <= 1<<33: //8G
  24. blockSize = 1 << 22 //4mb
  25. case fileSize <= 1<<34: //16g
  26. blockSize = 1 << 23 //8mb
  27. default:
  28. blockSize = 1 << 24 //16mb
  29. }
  30. temp := float64(fileSize) / float64(blockSize)
  31. length = int(math.Ceil(temp))
  32. return
  33. }
  34. //get the hash of the data
  35. func GetHashByContent(data []byte) string {
  36. sum := md5.Sum(data)
  37. return hex.EncodeToString(sum[:])
  38. }
  39. //get the hash of the data
  40. func GetHashByPath(path string) string {
  41. pFile, err := os.Open(path)
  42. if err != nil {
  43. return ""
  44. }
  45. defer pFile.Close()
  46. md5h := md5.New()
  47. io.Copy(md5h, pFile)
  48. return hex.EncodeToString(md5h.Sum(nil))
  49. }
  50. //Comparison data hash
  51. func ComparisonHash(data []byte, hash string) bool {
  52. sum := md5.Sum(data)
  53. return hex.EncodeToString(sum[:]) == hash
  54. }
  55. //get prefix byte length
  56. func PrefixLength(byteLength int) []byte {
  57. lengthByte := []byte{'0', '0', '0', '0', '0', '0'}
  58. bSize := strconv.Itoa(byteLength)
  59. cha := 6 - len(bSize)
  60. for i := len(bSize); i > 0; i-- {
  61. lengthByte[cha+i-1] = bSize[i-1]
  62. }
  63. return lengthByte
  64. }
  65. //get data byte length
  66. func DataLength(length int) []byte {
  67. lengthByte := []byte{'0', '0', '0', '0', '0', '0', '0', '0'}
  68. bSize := strconv.Itoa(length)
  69. cha := 8 - len(bSize)
  70. for i := len(bSize); i > 0; i-- {
  71. lengthByte[cha+i-1] = bSize[i-1]
  72. }
  73. return lengthByte
  74. }