github.com/gogf/gf/v2@v2.7.4/encoding/ghash/ghash_jshash.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  // JS implements the classic JS hash algorithm for 32 bits.
    10  func JS(str []byte) uint32 {
    11  	var hash uint32 = 1315423911
    12  	for i := 0; i < len(str); i++ {
    13  		hash ^= (hash << 5) + uint32(str[i]) + (hash >> 2)
    14  	}
    15  	return hash
    16  }
    17  
    18  // JS64 implements the classic JS hash algorithm for 64 bits.
    19  func JS64(str []byte) uint64 {
    20  	var hash uint64 = 1315423911
    21  	for i := 0; i < len(str); i++ {
    22  		hash ^= (hash << 5) + uint64(str[i]) + (hash >> 2)
    23  	}
    24  	return hash
    25  }