Created
June 29, 2023 03:28
-
-
Save zed-wong/605b0e71951ccc412cbb7ae8ab826c4b to your computer and use it in GitHub Desktop.
mixin-unique-conversation-id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://go.dev/play/ | |
package main | |
import ( | |
"crypto/md5" | |
"io" | |
"strings" | |
"github.com/gofrs/uuid" | |
) | |
func UniqueConversationId(userId, recipientId string) string { | |
minId, maxId := userId, recipientId | |
if strings.Compare(userId, recipientId) > 0 { | |
maxId, minId = userId, recipientId | |
} | |
h := md5.New() | |
io.WriteString(h, minId) | |
io.WriteString(h, maxId) | |
sum := h.Sum(nil) | |
sum[6] = (sum[6] & 0x0f) | 0x30 | |
sum[8] = (sum[8] & 0x3f) | 0x80 | |
return uuid.FromBytesOrNil(sum).String() | |
} | |
func main() { | |
println(UniqueConversationId("", "")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment