Path: blob/main/vendor/github.com/google/uuid/hash.go
2875 views
// Copyright 2016 Google Inc. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package uuid56import (7"crypto/md5"8"crypto/sha1"9"hash"10)1112// Well known namespace IDs and UUIDs13var (14NameSpaceDNS = Must(Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8"))15NameSpaceURL = Must(Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8"))16NameSpaceOID = Must(Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8"))17NameSpaceX500 = Must(Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8"))18Nil UUID // empty UUID, all zeros1920// The Max UUID is special form of UUID that is specified to have all 128 bits set to 1.21Max = UUID{220xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,230xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,24}25)2627// NewHash returns a new UUID derived from the hash of space concatenated with28// data generated by h. The hash should be at least 16 byte in length. The29// first 16 bytes of the hash are used to form the UUID. The version of the30// UUID will be the lower 4 bits of version. NewHash is used to implement31// NewMD5 and NewSHA1.32func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID {33h.Reset()34h.Write(space[:]) //nolint:errcheck35h.Write(data) //nolint:errcheck36s := h.Sum(nil)37var uuid UUID38copy(uuid[:], s)39uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4)40uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant41return uuid42}4344// NewMD5 returns a new MD5 (Version 3) UUID based on the45// supplied name space and data. It is the same as calling:46//47// NewHash(md5.New(), space, data, 3)48func NewMD5(space UUID, data []byte) UUID {49return NewHash(md5.New(), space, data, 3)50}5152// NewSHA1 returns a new SHA1 (Version 5) UUID based on the53// supplied name space and data. It is the same as calling:54//55// NewHash(sha1.New(), space, data, 5)56func NewSHA1(space UUID, data []byte) UUID {57return NewHash(sha1.New(), space, data, 5)58}596061