Skip to content

Instantly share code, notes, and snippets.

@giautm
Created November 2, 2021 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giautm/10bb2cb165477a7a34d5ea71362b6641 to your computer and use it in GitHub Desktop.
Save giautm/10bb2cb165477a7a34d5ea71362b6641 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"crypto/md5"
"github.com/google/uuid"
)
func CodeToUUID(code string) uuid.UUID {
return uuid.NewHash(md5.New(), uuid.Nil, []byte(code), 4)
}
func main() {
fmt.Println(CodeToUUID("Hello, playground"))
}
<?php
function guidv4($data = null) {
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);
// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
function codeToUUID($code, $namespace = null) {
$ctx = hash_init('md5');
if (is_null($namespace)) {
hash_update($ctx, hex2bin('00000000000000000000000000000000'));
} else {
hash_update($ctx, $namespace);
}
hash_update($ctx, $code);
return guidv4(hash_final($ctx, true));
}
echo codeToUUID("Hello, playground");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment