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