ghash_ap.go 893 B

123456789101112131415161718192021222324252627282930313233
  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. // AP implements the classic AP hash algorithm for 32 bits.
  8. func AP(str []byte) uint32 {
  9. var hash uint32
  10. for i := 0; i < len(str); i++ {
  11. if (i & 1) == 0 {
  12. hash ^= (hash << 7) ^ uint32(str[i]) ^ (hash >> 3)
  13. } else {
  14. hash ^= ^((hash << 11) ^ uint32(str[i]) ^ (hash >> 5)) + 1
  15. }
  16. }
  17. return hash
  18. }
  19. // AP64 implements the classic AP hash algorithm for 64 bits.
  20. func AP64(str []byte) uint64 {
  21. var hash uint64
  22. for i := 0; i < len(str); i++ {
  23. if (i & 1) == 0 {
  24. hash ^= (hash << 7) ^ uint64(str[i]) ^ (hash >> 3)
  25. } else {
  26. hash ^= ^((hash << 11) ^ uint64(str[i]) ^ (hash >> 5)) + 1
  27. }
  28. }
  29. return hash
  30. }