如何将多个字符串组合成一个唯一的“压缩”字符串?

huangapple go评论70阅读模式
英文:

Go - How to Combine Multiple Strings To Output a Unique 'Compressed' String?

问题

在Go语言中,我正在尝试将一个IP地址与一个用户名混合在一起,以输出某种压缩的唯一字符串。

例如: "MyUsername" + "192.354.32.245" = "JDU7DNd"

英文:

In Go, I'm trying to mix an IP address with a username to output some sort of compressed unique string.

For example: "MyUsername" + "192.354.32.245" = "JDU7DNd"

答案1

得分: 2

每个用户可以生成一个**UUID**,它将生成一个随机的128位值。

然而,为了得到一个固定的输出,我编写了一个函数,它将哈希和求和多个字符串,输出一个唯一的值。

func Combine(string ...string) uint32 {
    h := fnv.New32a()

    for _, a := range string {
        h.Write([]byte(a))
    }

    return h.Sum32()
}

https://play.golang.org/p/W_6GUTRJ6b


贡献者:
Martin Gallagher (fnv), nevets (uuid).

英文:

A UUID can be generated for each user which will generate a random 128 bit value.

However, for a static output, I have written a function which will hash and sum multiple strings which will output a unique value.

func Combine(string ...string) uint32 {
	h := fnv.New32a()

	for _, a := range string {
		h.Write([]byte(a))
	}

	return h.Sum32()
}

https://play.golang.org/p/W_6GUTRJ6b


Contributors:
Martin Gallagher (fnv), nevets (uuid).

huangapple
  • 本文由 发表于 2016年4月5日 21:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/36428392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定