Beispiel #1
0
// genNewID generates a new, unique device ID
func genNewRandID(p *page.Params, d *ds.Device) {
	b := make([]byte, 9) // Use 9 bytes: multiple of 3 bytes (ideal for base64 encoding so no padding '=' signs will be needed)
	if _, p.Err = rand.Read(b); p.Err != nil {
		return
	}

	RandID := base64.URLEncoding.EncodeToString(b)

	// Check if RandID is unique.
	// It will be, but once in a million years we might maybe perhaps encounter a match!
	q := datastore.NewQuery(ds.ENameDevice).Filter(ds.PNameRandID+"=", RandID).KeysOnly().Limit(1)
	var devKeys []*datastore.Key
	if devKeys, p.Err = q.GetAll(p.AppCtx, nil); p.Err != nil {
		return
	}
	if len(devKeys) > 0 {
		p.Err = fmt.Errorf("Generated device RandID already exists: %s", RandID)
		return
	}

	d.RandID = RandID
}