Skip to content
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

Switch to golang's crypto/rand #12

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gotp

import (
"fmt"
"math/rand"
"crypto/rand"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -77,14 +77,11 @@ func Itob(integer int) []byte {

// generate a random secret of given length
func RandomSecret(length int) string {
rand.Seed(time.Now().UnixNano())
letterRunes := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")

bytes := make([]rune, length)

for i := range bytes {
bytes[i] = letterRunes[rand.Intn(len(letterRunes))]
s := make([]byte, length*10)
_, err := rand.Read(s)
if err != nil {
log.Println("Unable to generate secret; insufficient entropy?",err)
return ""
}

return string(bytes)
return hex.EncodeToString(s[:length])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random secret should be base32 format according to OTP.secret.

Copy link
Author

@codewinch codewinch Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 bytes with that constrained alphabet (base32) is probably not a large enough search space to be secure.

https://datatracker.ietf.org/doc/html/rfc4226#appendix-A.4.1
https://datatracker.ietf.org/doc/html/rfc6238
https://datatracker.ietf.org/doc/html/rfc4086#page-34

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from https://github.com/pquerna/otp/blob/60112ee2a95553a491c22956619ef30260ec93e8/totp/totp.go#L150

var b32NoPadding = base32.StdEncoding.WithPadding(base32.NoPadding)
b32NoPadding(EncodeToString(secret))

Still needs to be 32 bytes though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to figure out how to edit this PR in the web UI without actually going to the trouble of cloning it locally haha

}