ghash_rs.go 787 B

1234567891011121314151617181920212223242526272829303132333435
  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. // RS implements the classic RS hash algorithm for 32 bits.
  8. func RS(str []byte) uint32 {
  9. var (
  10. b uint32 = 378551
  11. a uint32 = 63689
  12. hash uint32 = 0
  13. )
  14. for i := 0; i < len(str); i++ {
  15. hash = hash*a + uint32(str[i])
  16. a *= b
  17. }
  18. return hash
  19. }
  20. // RS64 implements the classic RS hash algorithm for 64 bits.
  21. func RS64(str []byte) uint64 {
  22. var (
  23. b uint64 = 378551
  24. a uint64 = 63689
  25. hash uint64 = 0
  26. )
  27. for i := 0; i < len(str); i++ {
  28. hash = hash*a + uint64(str[i])
  29. a *= b
  30. }
  31. return hash
  32. }