github.com/wangyougui/gf/v2@v2.6.5/encoding/ghash/ghash_elf.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  // ELF implements the classic ELF hash algorithm for 32 bits.
    10  func ELF(str []byte) uint32 {
    11  	var (
    12  		hash uint32
    13  		x    uint32
    14  	)
    15  	for i := 0; i < len(str); i++ {
    16  		hash = (hash << 4) + uint32(str[i])
    17  		if x = hash & 0xF0000000; x != 0 {
    18  			hash ^= x >> 24
    19  			hash &= ^x + 1
    20  		}
    21  	}
    22  	return hash
    23  }
    24  
    25  // ELF64 implements the classic ELF hash algorithm for 64 bits.
    26  func ELF64(str []byte) uint64 {
    27  	var (
    28  		hash uint64
    29  		x    uint64
    30  	)
    31  	for i := 0; i < len(str); i++ {
    32  		hash = (hash << 4) + uint64(str[i])
    33  		if x = hash & 0xF000000000000000; x != 0 {
    34  			hash ^= x >> 24
    35  			hash &= ^x + 1
    36  		}
    37  	}
    38  	return hash
    39  }