github.com/orangebees/go-oneutils@v0.0.10/GlobalStore/Integrity.go (about)

     1  package GlobalStore
     2  
     3  import (
     4  	"crypto/md5"
     5  	"crypto/sha1"
     6  	"crypto/sha256"
     7  	"crypto/sha512"
     8  	"encoding/base64"
     9  	"encoding/hex"
    10  	"github.com/cespare/xxhash/v2"
    11  )
    12  
    13  const hextable = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    14  
    15  type Integrity string
    16  
    17  // GetType 获取hash类型
    18  func (it Integrity) GetType() string {
    19  	for i := 0; i < len(it); i++ {
    20  		if it[i] == '-' {
    21  			return string(it[:i])
    22  		}
    23  	}
    24  	return ""
    25  }
    26  
    27  // GetValue 获取直接来自Integrity的值(hash+base64计算得到)
    28  func (it Integrity) GetValue() string {
    29  	itlen := len(it)
    30  	for i := 0; i < itlen; i++ {
    31  		if it[i] == '-' {
    32  			if itlen > i+1 {
    33  				return string(it[i+1:])
    34  			}
    35  			return ""
    36  		}
    37  	}
    38  	return ""
    39  }
    40  
    41  // GetRawHashValue 获取原始hash值
    42  func (it Integrity) GetRawHashValue() ([]byte, error) {
    43  	decodeString, err := base64.StdEncoding.DecodeString(it.GetValue())
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return decodeString, nil
    48  }
    49  
    50  // GetRawHashString 获取原始hash字符串值 hash to string
    51  func (it Integrity) GetRawHashString() (string, error) {
    52  	value, err := it.GetRawHashValue()
    53  	if err != nil {
    54  		return "", err
    55  	}
    56  	return hex.EncodeToString(value), nil
    57  }
    58  func (it Integrity) GetRawHashStringAndMod256() (string, string, error) {
    59  	hashRawValue, err := it.GetRawHashValue()
    60  	if err != nil {
    61  		return "", "", err
    62  	}
    63  	t := xxhash.Sum64(hashRawValue) & 255
    64  	return hex.EncodeToString(hashRawValue), string([]byte{hextable[t>>4], hextable[t&15]}), nil
    65  }
    66  func (it Integrity) GetRawHashStringAndMod(i uint64) (string, string, error) {
    67  	hashRawValue, err := it.GetRawHashValue()
    68  	if err != nil {
    69  		return "", "", err
    70  	}
    71  	t := xxhash.Sum64(hashRawValue) % i
    72  	return hex.EncodeToString(hashRawValue), string([]byte{hextable[t>>4], hextable[t&15]}), nil
    73  }
    74  
    75  // GetRawHashStringAndModFast 获取原始hash字符串并且快速取模(i为16的n次方)
    76  func (it Integrity) GetRawHashStringAndModFast(i uint64) (string, string, error) {
    77  	hashRawValue, err := it.GetRawHashValue()
    78  	if err != nil {
    79  		return "", "", err
    80  	}
    81  	t := xxhash.Sum64(hashRawValue) & (i - 1)
    82  	return hex.EncodeToString(hashRawValue), string([]byte{hextable[t>>4], hextable[t&15]}), nil
    83  }
    84  
    85  // NewIntegrity 新建Integrity实例
    86  func NewIntegrity(integrityType string, data []byte) Integrity {
    87  	var integrityBytes = make([]byte, 64)
    88  	integrityBytes = integrityBytes[:0]
    89  	switch integrityType {
    90  	case "md5":
    91  		t := md5.Sum(data)
    92  		integrityBytes = append(integrityBytes, t[:]...)
    93  	case "sha1":
    94  		t := sha1.Sum(data)
    95  		integrityBytes = append(integrityBytes, t[:]...)
    96  	case "sha256":
    97  		t := sha256.Sum256(data)
    98  		integrityBytes = append(integrityBytes, t[:]...)
    99  	case "sha512":
   100  		t := sha512.Sum512(data)
   101  		integrityBytes = append(integrityBytes, t[:]...)
   102  	}
   103  
   104  	return Integrity(integrityType + "-" + base64.StdEncoding.EncodeToString(integrityBytes))
   105  }