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