2015-09-05 11:48:38 -04:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2015-09-07 17:48:37 -04:00
|
|
|
"crypto/md5"
|
2015-09-06 16:10:42 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2015-09-05 11:48:38 -04:00
|
|
|
)
|
|
|
|
|
2015-09-07 06:00:46 -04:00
|
|
|
// The ID of en entity, in the form of an UUID.
|
2015-09-05 11:48:38 -04:00
|
|
|
type VID [16]byte
|
|
|
|
|
2015-09-08 07:18:55 -04:00
|
|
|
// Hash generates a MD5 hash based on current VID and a suffix string.
|
2015-09-07 17:48:19 -04:00
|
|
|
func (v VID) Hash(suffix []byte) []byte {
|
2015-09-07 17:48:37 -04:00
|
|
|
md5 := md5.New()
|
|
|
|
md5.Write(v[:])
|
|
|
|
md5.Write(suffix)
|
|
|
|
return md5.Sum(nil)
|
2015-09-07 17:48:19 -04:00
|
|
|
}
|
|
|
|
|
2015-09-05 11:48:38 -04:00
|
|
|
var byteGroups = []int{8, 4, 4, 4, 12}
|
|
|
|
|
|
|
|
// TODO: leverage a full functional UUID library
|
|
|
|
func UUIDToVID(uuid string) (v VID, err error) {
|
2015-09-06 16:10:42 -04:00
|
|
|
text := []byte(uuid)
|
|
|
|
if len(text) < 32 {
|
2015-09-05 11:48:38 -04:00
|
|
|
err = fmt.Errorf("uuid: invalid UUID string: %s", text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b := v[:]
|
|
|
|
|
|
|
|
for _, byteGroup := range byteGroups {
|
|
|
|
if text[0] == '-' {
|
|
|
|
text = text[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
text = text[byteGroup:]
|
|
|
|
b = b[byteGroup/2:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|