github.com/rudderlabs/rudder-go-kit@v0.30.0/uuid/uuid.go (about)

     1  package uuid
     2  
     3  import (
     4  	"crypto/md5"
     5  
     6  	"github.com/google/uuid"
     7  )
     8  
     9  // GetMD5UUID hashes the given string into md5 and returns it as uuid
    10  func GetMD5UUID(str string) (uuid.UUID, error) {
    11  	// To maintain backward compatibility, we are using md5 hash of the string
    12  	// We are mimicking github.com/gofrs/uuid behavior:
    13  	//
    14  	// md5Sum := md5.Sum([]byte(str))
    15  	// u, err := uuid.FromBytes(md5Sum[:])
    16  
    17  	// u.SetVersion(uuid.V4)
    18  	// u.SetVariant(uuid.VariantRFC4122)
    19  
    20  	// google/uuid doesn't allow us to modify the version and variant,
    21  	// so we are doing it manually, using gofrs/uuid library implementation.
    22  	md5Sum := md5.Sum([]byte(str)) // skipcq: GO-S1023
    23  	// SetVariant: VariantRFC4122
    24  	md5Sum[8] = md5Sum[8]&(0xff>>2) | (0x02 << 6)
    25  	// SetVersion: Version 4
    26  	version := byte(4)
    27  	md5Sum[6] = (md5Sum[6] & 0x0f) | (version << 4)
    28  
    29  	return uuid.FromBytes(md5Sum[:])
    30  }