github.com/wangyougui/gf/v2@v2.6.5/encoding/ghash/ghash_ap.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/wangyougui/gf.
     6  
     7  package ghash
     8  
     9  // AP implements the classic AP hash algorithm for 32 bits.
    10  func AP(str []byte) uint32 {
    11  	var hash uint32
    12  	for i := 0; i < len(str); i++ {
    13  		if (i & 1) == 0 {
    14  			hash ^= (hash << 7) ^ uint32(str[i]) ^ (hash >> 3)
    15  		} else {
    16  			hash ^= ^((hash << 11) ^ uint32(str[i]) ^ (hash >> 5)) + 1
    17  		}
    18  	}
    19  	return hash
    20  }
    21  
    22  // AP64 implements the classic AP hash algorithm for 64 bits.
    23  func AP64(str []byte) uint64 {
    24  	var hash uint64
    25  	for i := 0; i < len(str); i++ {
    26  		if (i & 1) == 0 {
    27  			hash ^= (hash << 7) ^ uint64(str[i]) ^ (hash >> 3)
    28  		} else {
    29  			hash ^= ^((hash << 11) ^ uint64(str[i]) ^ (hash >> 5)) + 1
    30  		}
    31  	}
    32  	return hash
    33  }