57 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
		
		
			
		
	
	
			57 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| 
								 | 
							
								PACKAGE
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								package uniuri
							 | 
						||
| 
								 | 
							
								import "github.com/dchest/uniuri"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Package uniuri generates random strings good for use in URIs to identify
							 | 
						||
| 
								 | 
							
								unique objects.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Example usage:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									s := uniuri.New() // s is now "apHCJBl7L1OmC57n"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								A standard string created by New() is 16 bytes in length and consists of
							 | 
						||
| 
								 | 
							
								Latin upper and lowercase letters, and numbers (from the set of 62 allowed
							 | 
						||
| 
								 | 
							
								characters), which means that it has ~95 bits of entropy. To get more
							 | 
						||
| 
								 | 
							
								entropy, you can use NewLen(UUIDLen), which returns 20-byte string, giving
							 | 
						||
| 
								 | 
							
								~119 bits of entropy, or any other desired length.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Functions read from crypto/rand random source, and panic if they fail to
							 | 
						||
| 
								 | 
							
								read from it.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								CONSTANTS
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const (
							 | 
						||
| 
								 | 
							
								    // Standard length of uniuri string to achive ~95 bits of entropy.
							 | 
						||
| 
								 | 
							
								    StdLen = 16
							 | 
						||
| 
								 | 
							
								    // Length of uniurl string to achive ~119 bits of entropy, closest
							 | 
						||
| 
								 | 
							
								    // to what can be losslessly converted to UUIDv4 (122 bits).
							 | 
						||
| 
								 | 
							
								    UUIDLen = 20
							 | 
						||
| 
								 | 
							
								)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								VARIABLES
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								var StdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Standard characters allowed in uniuri string.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								FUNCTIONS
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func New() string
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								New returns a new random string with the standard length and standard
							 | 
						||
| 
								 | 
							
								characters.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func NewLen(length int) string
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								NewLen returns a new random string with the provided length and standard
							 | 
						||
| 
								 | 
							
								characters.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func NewLenChars(length int, chars []byte) string
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								NewLenChars returns a new random string with the provided length and byte
							 | 
						||
| 
								 | 
							
								slice of allowed characters (maximum 256).
							 |