import (
    "crypto/md5"
    "encoding/hex"
    "encoding/json"
    "fmt"
    "hash/crc32"
    "hash/fnv"
    "strconv"
)

//string to hash
//字符串转化为hash值   使用的是 IEEE 
func MakeHash(s string) string {
    const IEEE = 0xedb88320
    var IEEETable = crc32.MakeTable(IEEE) //创建IEEE table
    hash := fmt.Sprintf("%x", crc32.Checksum([]byte(s), IEEETable))
    return hash
}
//字符串转化为hash值
func HashString(encode string) uint64 {
    hash := fnv.New64()
    hash.Write([]byte(encode))
    return hash.Sum64()
}

// 制作特征值方法一
func MakeUnique(obj interface{}) string {
    baseString, _ := json.Marshal(obj)
    return strconv.FormatUint(HashString(string(baseString)), 10)
}

// 制作特征值方法二
func MakeMd5(obj interface{}, length int) string {
    if length > 32 {
        length = 32
    }
    h := md5.New()
    baseString, _ := json.Marshal(obj)
    h.Write([]byte(baseString))
    s := hex.EncodeToString(h.Sum(nil))//转化为16进制字符串
    return s[:length]
}