Skip to content

Instantly share code, notes, and snippets.

@zed-wong
Created June 29, 2023 03:28
Show Gist options
  • Save zed-wong/605b0e71951ccc412cbb7ae8ab826c4b to your computer and use it in GitHub Desktop.
Save zed-wong/605b0e71951ccc412cbb7ae8ab826c4b to your computer and use it in GitHub Desktop.
mixin-unique-conversation-id
//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