ghash_jshash.go 723 B

12345678910111213141516171819202122232425
  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. // JS implements the classic JS hash algorithm for 32 bits.
  8. func JS(str []byte) uint32 {
  9. var hash uint32 = 1315423911
  10. for i := 0; i < len(str); i++ {
  11. hash ^= (hash << 5) + uint32(str[i]) + (hash >> 2)
  12. }
  13. return hash
  14. }
  15. // JS64 implements the classic JS hash algorithm for 64 bits.
  16. func JS64(str []byte) uint64 {
  17. var hash uint64 = 1315423911
  18. for i := 0; i < len(str); i++ {
  19. hash ^= (hash << 5) + uint64(str[i]) + (hash >> 2)
  20. }
  21. return hash
  22. }