This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2242 from njhale/parameterize-basic-secret-gen
Parameterize basic secret generation (#2192)
- Loading branch information
Showing
8 changed files
with
93 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package secrets | ||
|
||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
) | ||
|
||
const ( | ||
defaultLength = 54 | ||
defaultCharacterSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%^&*_-=+" | ||
) | ||
|
||
// GenerateRandomSecret generates a random secret with the specified length and character set. | ||
// If the length is less than 1, a default value of 54 will be used. | ||
// If the character set is empty, a default value of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%^&*_-=+" | ||
// will be used. | ||
func GenerateRandomSecret(length int, characterSet string) (string, error) { | ||
if length < 1 { | ||
length = defaultLength | ||
} | ||
if characterSet == "" { | ||
characterSet = defaultCharacterSet | ||
} | ||
|
||
// Generate a random secret by randomly selecting characters from the given character set. | ||
secret := make([]byte, length) | ||
for i := 0; i < length; i++ { | ||
index, err := rand.Int(rand.Reader, big.NewInt(int64(len(characterSet)))) | ||
if err != nil { | ||
return "", err | ||
} | ||
secret[i] = characterSet[index.Int64()] | ||
} | ||
|
||
return string(secret), nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters