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.

76 lines
1.2 KiB

  1. package util
  2. import (
  3. "crypto/md5"
  4. "io"
  5. )
  6. // big endian
  7. func BytesToUint64(b []byte) (v uint64) {
  8. length := uint(len(b))
  9. for i := uint(0); i < length-1; i++ {
  10. v += uint64(b[i])
  11. v <<= 8
  12. }
  13. v += uint64(b[length-1])
  14. return
  15. }
  16. func BytesToUint32(b []byte) (v uint32) {
  17. length := uint(len(b))
  18. for i := uint(0); i < length-1; i++ {
  19. v += uint32(b[i])
  20. v <<= 8
  21. }
  22. v += uint32(b[length-1])
  23. return
  24. }
  25. func BytesToUint16(b []byte) (v uint16) {
  26. v += uint16(b[0])
  27. v <<= 8
  28. v += uint16(b[1])
  29. return
  30. }
  31. func Uint64toBytes(b []byte, v uint64) {
  32. for i := uint(0); i < 8; i++ {
  33. b[7-i] = byte(v >> (i * 8))
  34. }
  35. }
  36. func Uint32toBytes(b []byte, v uint32) {
  37. for i := uint(0); i < 4; i++ {
  38. b[3-i] = byte(v >> (i * 8))
  39. }
  40. }
  41. func Uint16toBytes(b []byte, v uint16) {
  42. b[0] = byte(v >> 8)
  43. b[1] = byte(v)
  44. }
  45. func Uint8toBytes(b []byte, v uint8) {
  46. b[0] = byte(v)
  47. }
  48. // returns a 64 bit big int
  49. func HashStringToLong(dir string) (v int64) {
  50. h := md5.New()
  51. io.WriteString(h, dir)
  52. b := h.Sum(nil)
  53. v += int64(b[0])
  54. v <<= 8
  55. v += int64(b[1])
  56. v <<= 8
  57. v += int64(b[2])
  58. v <<= 8
  59. v += int64(b[3])
  60. v <<= 8
  61. v += int64(b[4])
  62. v <<= 8
  63. v += int64(b[5])
  64. v <<= 8
  65. v += int64(b[6])
  66. v <<= 8
  67. v += int64(b[7])
  68. return
  69. }