ghash_elf.go 879 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package ghash
  7. // ELF implements the classic ELF hash algorithm for 32 bits.
  8. func ELF(str []byte) uint32 {
  9. var (
  10. hash uint32
  11. x uint32
  12. )
  13. for i := 0; i < len(str); i++ {
  14. hash = (hash << 4) + uint32(str[i])
  15. if x = hash & 0xF0000000; x != 0 {
  16. hash ^= x >> 24
  17. hash &= ^x + 1
  18. }
  19. }
  20. return hash
  21. }
  22. // ELF64 implements the classic ELF hash algorithm for 64 bits.
  23. func ELF64(str []byte) uint64 {
  24. var (
  25. hash uint64
  26. x uint64
  27. )
  28. for i := 0; i < len(str); i++ {
  29. hash = (hash << 4) + uint64(str[i])
  30. if x = hash & 0xF000000000000000; x != 0 {
  31. hash ^= x >> 24
  32. hash &= ^x + 1
  33. }
  34. }
  35. return hash
  36. }