(t time.Time)
| 215 | } |
| 216 | |
| 217 | func NewRandomWithTime(t time.Time) (ksuid KSUID, err error) { |
| 218 | // Go's default random number generators are not safe for concurrent use by |
| 219 | // multiple goroutines, the use of the rander and randBuffer are explicitly |
| 220 | // synchronized here. |
| 221 | randMutex.Lock() |
| 222 | |
| 223 | _, err = io.ReadAtLeast(rander, randBuffer[:], len(randBuffer)) |
| 224 | copy(ksuid[timestampLengthInBytes:], randBuffer[:]) |
| 225 | |
| 226 | randMutex.Unlock() |
| 227 | |
| 228 | if err != nil { |
| 229 | ksuid = Nil // don't leak random bytes on error |
| 230 | return |
| 231 | } |
| 232 | |
| 233 | ts := timeToCorrectedUTCTimestamp(t) |
| 234 | binary.BigEndian.PutUint32(ksuid[:timestampLengthInBytes], ts) |
| 235 | return |
| 236 | } |
| 237 | |
| 238 | // Constructs a KSUID from constituent parts |
| 239 | func FromParts(t time.Time, payload []byte) (KSUID, error) { |
searching dependent graphs…