// generateID creates a new random string identifier with the given length
432755
func generateID(l int) string {
	const (
		// ensures we backoff for less than 450ms total. Use the following to
		// select new value, in units of 10ms:
		// 	n*(n+1)/2 = d -> n^2 + n - 2d -> n = (sqrt(8d + 1) - 1)/2
		maxretries = 9
		backoff    = time.Millisecond * 10
	)

	var (
		totalBackoff time.Duration
		count        int
		retries      int
		size         = (l*5 + 7) / 8
		u            = make([]byte, size)
	)
	// TODO: Include time component, counter component, random component

	for {
		// This should never block but the read may fail. Because of this,
		// we just try to read the random number generator until we get
		// something. This is a very rare condition but may happen.
		b := time.Duration(retries) * backoff
		time.Sleep(b)
		totalBackoff += b

		n, err := io.ReadFull(rand.Reader, u[count:])
		if err != nil {
			if retryOnError(err) && retries < maxretries {
				count += n
				retries++
				logger.Errorf("error generating version 4 uuid, retrying: %v", err)
				continue
			}

			// Any other errors represent a system problem. What did someone
			// do to /dev/urandom?
			panic(fmt.Errorf("error reading random number generator, retried for %v: %v", totalBackoff.String(), err))
		}

		break
	}

	s := base32.StdEncoding.EncodeToString(u)

	return s[:l]
}
189479
func FakeID(b byte) ID {
	dh := kbfshash.RawDefaultHash{b}
	h, err := kbfshash.HashFromRaw(kbfshash.DefaultHashType, dh[:])
	if err != nil {
		panic(err)
	}
	return ID{h}
}
249078
func NewRequestID() (string, error) {
	id, err := uuid.NewV4()
	if err != nil {
		return "", err
	}

	return strings.Replace(id.String(), "-", "", -1), nil
}
1011265
func (s *IngestPutPipelineService) Id(id string) *IngestPutPipelineService {
	s.id = id
	return s
}
1060163
func (mpi *MempoolInfo) ID() uint64 {
	mpi.RLock()
	defer mpi.RUnlock()
	return mpi.Ident
}
486580
func (s *GetTemplateService) Id(id string) *GetTemplateService {
	s.id = id
	return s
}
1328233
func Random() ID {
	id, err := Read(uuid.NewRandom())
	if err != nil {
		panic(fmt.Sprintf("Unable to generate random peer id: %s", err))
	}
	return id
}
865515
func (p Prefixed) Generate() string {
	if p.Len == 0 {
		p.Len = PrefixedDefaultLen
	}

	if len(p.Acceptable) == 0 {
		p.Acceptable = PrefixedDefaultAcceptable
	}

	if p.RandGenerator == nil {
		p.RandGenerator = rand.Reader
	}

	builder := bytes.NewBufferString(p.Prefix)

	max := big.NewInt(int64(len(p.Acceptable)))

	for i := uint8(0); i < p.Len; i++ {
		result, err := rand.Int(p.RandGenerator, max)
		if err != nil {
			panic(err)
		}
		builder.WriteRune(p.Acceptable[result.Int64()])
	}
	return builder.String()
}
233009
func (k *TUFKey) ID() string {
	if k.id == "" {
		pubK := TUFKey{
			Type: k.Algorithm(),
			Value: KeyPair{
				Public:  k.Public(),
				Private: nil,
			},
		}
		data, err := json.MarshalCanonical(&pubK)
		if err != nil {
			logrus.Error("Error generating key ID:", err)
		}
		digest := sha256.Sum256(data)
		k.id = hex.EncodeToString(digest[:])
	}
	return k.id
}
639224
func (e *Epoch) ID() EpochID {
	base := e.Base()
	var id EpochID
	binary.LittleEndian.PutUint64(id[:], base)
	id[7] = e.Level
	return id
}
1371532
func RandomID(a ID, n int) (b ID) {
	if n == 0 {
		return a
	}
	// flip bit at position n, fill the rest with random bits
	b = a
	pos := len(a) - n/8 - 1
	bit := byte(0x01) << (byte(n%8) - 1)
	if bit == 0 {
		pos++
		bit = 0x80
	}
	b[pos] = a[pos]&^bit | ^a[pos]&bit // TODO: randomize end bits
	for i := pos + 1; i < len(a); i++ {
		b[i] = byte(rand.Intn(255))
	}
	return b
}
1190612
func (s *SliceQuery) Id(id int) *SliceQuery {
	s.id = &id
	return s
}
996376
func (t *FpdfTpl) ID() string {
	return fmt.Sprintf("%x", sha1.Sum(t.Bytes()))
}
617160
func (media *FeedMedia) ID() string {
	switch s := media.NextID.(type) {
	case string:
		return s
	case int64:
		return strconv.FormatInt(s, 10)
	case json.Number:
		return string(s)
	}
	return ""
}
150129
func (p *Params) Generate(id string) string {
	buf := make([]byte, dataSize, dataSize+macSize)
	binary.BigEndian.PutUint32(buf, uint32(p.timer().Unix()))
	token := append(buf, hmacSHA256(p.key, buf, id)...)
	return base64.URLEncoding.EncodeToString(token)
}
661167
func generateID(mutex *sync.Mutex, ids map[uint16]bool) uint16 {
	mutex.Lock()
	id := uint16(1)
	for ; id <= math.MaxUint16; id++ {
		if _, ok := ids[id]; !ok {
			break
		}
	}
	ids[id] = true
	mutex.Unlock()
	return id
}
788598
func (self *Rain) Id() string {
	return fmt.Sprintf("%02x:%02x", self.id>>8, self.id&0xff)
}