github.com/gogf/gf/v2@v2.7.4/encoding/ghash/ghash_djb.go (about)

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