ghash_bkdr.go 805 B

12345678910111213141516171819202122232425262728293031
  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. // BKDR implements the classic BKDR hash algorithm for 32 bits.
  8. func BKDR(str []byte) uint32 {
  9. var (
  10. seed uint32 = 131 // 31 131 1313 13131 131313 etc..
  11. hash uint32 = 0
  12. )
  13. for i := 0; i < len(str); i++ {
  14. hash = hash*seed + uint32(str[i])
  15. }
  16. return hash
  17. }
  18. // BKDR64 implements the classic BKDR hash algorithm for 64 bits.
  19. func BKDR64(str []byte) uint64 {
  20. var (
  21. seed uint64 = 131 // 31 131 1313 13131 131313 etc..
  22. hash uint64 = 0
  23. )
  24. for i := 0; i < len(str); i++ {
  25. hash = hash*seed + uint64(str[i])
  26. }
  27. return hash
  28. }