mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 10:37:24 -04:00 
			
		
		
		
	context: RandomString action produces a random string of random length
This commit is contained in:
		
							parent
							
								
									f415ea263e
								
							
						
					
					
						commit
						edf9cd34cc
					
				| @ -4,6 +4,7 @@ import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"math/rand" | ||||
| 	"net" | ||||
| 	"net/http" | ||||
| 	"net/url" | ||||
| @ -353,3 +354,37 @@ func (c Context) IsMITM() bool { | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
| 
 | ||||
| // RandomString generates a random string of random length given | ||||
| // length bounds. Thanks to http://stackoverflow.com/a/31832326/1048862 | ||||
| // for the clever technique that is fast and maintains proper | ||||
| // distributions over the dictionary. | ||||
| func (c Context) RandomString(minLen, maxLen int) string { | ||||
| 	const ( | ||||
| 		letterBytes   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||||
| 		letterIdxBits = 6                    // 6 bits to represent a letter index | ||||
| 		letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits | ||||
| 		letterIdxMax  = 63 / letterIdxBits   // # of letter indices fitting in 63 bits | ||||
| 	) | ||||
| 
 | ||||
| 	if minLen < 0 || maxLen < 0 || maxLen <= minLen { | ||||
| 		return "" | ||||
| 	} | ||||
| 
 | ||||
| 	src := rand.NewSource(time.Now().UnixNano()) | ||||
| 	n := rand.Intn(maxLen-minLen) + minLen // choose actual length | ||||
| 	b := make([]byte, n) | ||||
| 	for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { | ||||
| 		if remain == 0 { | ||||
| 			cache, remain = src.Int63(), letterIdxMax | ||||
| 		} | ||||
| 		if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | ||||
| 			b[i] = letterBytes[idx] | ||||
| 			i-- | ||||
| 		} | ||||
| 		cache >>= letterIdxBits | ||||
| 		remain-- | ||||
| 	} | ||||
| 
 | ||||
| 	return string(b) | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user