diff options
author | FChannel <> | 2022-05-08 14:57:40 -0700 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 580dec5b89215310ce34341e11ff17fe38bdb63a (patch) | |
tree | 894424df66a9d9f7e41805822f29adac8fb490fe /util/key.go | |
parent | f7bf818d29393ceaccf4d2906557351fa6a4f49f (diff) |
more cleanup, logging and error logging everywhere
things are mostly in place can work on "features" and polish
Diffstat (limited to 'util/key.go')
-rw-r--r-- | util/key.go | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/util/key.go b/util/key.go index cd8662a..60eeb43 100644 --- a/util/key.go +++ b/util/key.go @@ -3,6 +3,7 @@ package util import ( "crypto/sha512" "encoding/hex" + "errors" "math/rand" "os" "strings" @@ -13,14 +14,14 @@ import ( const domain = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" -func CreateKey(len int) string { +func CreateKey(len int) (string, error) { // TODO: provided that CreateTripCode still uses sha512, the max len can be 128 at most. if len > 128 { - panic("len is greater than 128") // awful way to do it + return "", MakeError(errors.New("len is greater than 128"), "CreateKey") } str := CreateTripCode(RandomID(len)) - return str[:len] + return str[:len], nil } func CreateTripCode(input string) string { @@ -29,23 +30,13 @@ func CreateTripCode(input string) string { return hex.EncodeToString(out[:]) } -func RandomID(size int) string { - rng := size - newID := strings.Builder{} - for i := 0; i < rng; i++ { - newID.WriteByte(domain[rand.Intn(len(domain))]) - } - - return newID.String() -} - func GetCookieKey() (string, error) { if config.CookieKey == "" { var file *os.File var err error if file, err = os.OpenFile("config/config-init", os.O_APPEND|os.O_WRONLY, 0644); err != nil { - return "", err + return "", MakeError(err, "GetCookieKey") } defer file.Close() @@ -56,3 +47,14 @@ func GetCookieKey() (string, error) { return config.CookieKey, nil } + +func RandomID(size int) string { + rng := size + newID := strings.Builder{} + + for i := 0; i < rng; i++ { + newID.WriteByte(domain[rand.Intn(len(domain))]) + } + + return newID.String() +} |