This repository has been archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update token generation with crypto/rand (#40)
Co-authored-by: Frank Chiarulli Jr <[email protected]>
1 parent
2425061
commit 5f4dee1
Showing
4 changed files
with
19 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
package relay | ||
|
||
import "github.com/segmentio/ksuid" | ||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
) | ||
|
||
func GenerateToken() string { | ||
return ksuid.New().String() | ||
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | ||
|
||
func GenerateToken() (string, error) { | ||
length := 27 | ||
ret := make([]byte, length) | ||
for i := 0; i < length; i++ { | ||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(base62)))) | ||
if err != nil { | ||
return "", err | ||
} | ||
ret[i] = base62[num.Int64()] | ||
} | ||
return string(ret), nil | ||
} |