Source file src/crypto/rand/text.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rand
     6  
     7  const base32alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
     8  
     9  // Text returns a cryptographically random string using the standard RFC 4648 base32 alphabet
    10  // for use when a secret string, token, password, or other text is needed.
    11  // The result contains at least 128 bits of randomness, enough to prevent brute force
    12  // guessing attacks and to make the likelihood of collisions vanishingly small.
    13  // A future version may return longer texts as needed to maintain those properties.
    14  func Text() string {
    15  	// ⌈log₃₂ 2¹²⁸⌉ = 26 chars
    16  	src := make([]byte, 26)
    17  	Read(src)
    18  	for i := range src {
    19  		src[i] = base32alphabet[src[i]%32]
    20  	}
    21  	return string(src)
    22  }
    23  

View as plain text