-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Generate secure token using crypto rand #605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
ca5c89f
27c38e6
6fc33ad
e069bd2
0fbe374
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,9 @@ THE SOFTWARE. | |
| package server | ||
|
|
||
| import ( | ||
| "math/rand" | ||
| "crypto/rand" | ||
| "log" | ||
| "math/big" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -37,8 +39,11 @@ const ( | |
| func token(length int) string { | ||
| result := "" | ||
| for i := 0; i < length; i++ { | ||
| x := rand.Intn(len(SYMBOLS) - 1) | ||
| result = string(SYMBOLS[x]) + result | ||
| x, err := rand.Int(rand.Reader, big.NewInt(int64(len(SYMBOLS)))) | ||
| if err != nil { | ||
| log.Fatal("Failed to generate token") | ||
|
||
| } | ||
| result = string(SYMBOLS[x.Int64()]) + result | ||
| } | ||
|
|
||
| return result | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, will do it later. Should I pass logger into function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, thanks
you should pass a null logger in the test: https://github.com/dutchcoders/transfer.sh/blob/main/server/token_test.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it. But error message in logger in this function provide three error messages in log, because token calls several times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aspacca check pls