From 58ff28f84de5d76b3bb0123da83657311a4dc7e0 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 11 Jan 2019 20:25:39 +0000 Subject: [PATCH] feat: export generate key License: MIT Signed-off-by: Henrique Dias Former-commit-id: 3088476c44d4608e2b8c90d3c274b6db17ef30c0 [formerly e40d3ba193e0b54bccce8c13459032f51af84c7b] [formerly 33cabf26abee5ef4f1dd63e423fe1b0fa7ae4f7c [formerly a3daac84a20fd0d645527bcfd82d4ef46ecdf31c]] Former-commit-id: fe022f36fc4d7d8442df8c0fa4a86e2ff3ec328e [formerly 1d949d0ae88b3b5a9b8e4da5c3b6ca814a319589] Former-commit-id: 36207ddeb7eba335f5fe73ea9dcdf6bffc6265bf --- cmd/config_import.go | 2 +- cmd/config_init.go | 2 +- cmd/root.go | 2 +- cmd/utils.go | 10 ++++------ settings/settings.go | 13 +++++++++++++ 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/config_import.go b/cmd/config_import.go index 7182da9a..9fef477e 100644 --- a/cmd/config_import.go +++ b/cmd/config_import.go @@ -40,7 +40,7 @@ The path must be for a json or yaml file.`, checkErr(err) key = settings.Key } else { - key = generateRandomBytes(64) + key = generateKey() } file := settingsFile{} diff --git a/cmd/config_init.go b/cmd/config_init.go index f4794cb6..62b727a5 100644 --- a/cmd/config_init.go +++ b/cmd/config_init.go @@ -29,7 +29,7 @@ override the options.`, authMethod, auther := getAuthentication(flags) s := &settings.Settings{ - Key: generateRandomBytes(64), // 256 bit + Key: generateKey(), Signup: mustGetBool(flags, "signup"), Shell: strings.Split(strings.TrimSpace(mustGetString(flags, "shell")), " "), AuthMethod: authMethod, diff --git a/cmd/root.go b/cmd/root.go index 0c71df09..75b87278 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -215,7 +215,7 @@ func setupLog(logMethod string) { func quickSetup(flags *pflag.FlagSet, d pythonData) { set := &settings.Settings{ - Key: generateRandomBytes(64), // 256 bit + Key: generateKey(), Signup: false, Defaults: settings.UserDefaults{ Scope: ".", diff --git a/cmd/utils.go b/cmd/utils.go index 0c61bb69..65a3884e 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -1,7 +1,6 @@ package cmd import ( - "crypto/rand" "encoding/json" "errors" "fmt" @@ -10,6 +9,7 @@ import ( "path/filepath" "github.com/asdine/storm" + "github.com/filebrowser/filebrowser/v2/settings" "github.com/filebrowser/filebrowser/v2/storage" "github.com/filebrowser/filebrowser/v2/storage/bolt" "github.com/spf13/cobra" @@ -41,12 +41,10 @@ func mustGetUint(flags *pflag.FlagSet, flag string) uint { return b } -func generateRandomBytes(n int) []byte { - b := make([]byte, n) - _, err := rand.Read(b) +func generateKey() []byte { + k, err := settings.GenerateKey() checkErr(err) - // Note that err == nil only if we read len(b) bytes. - return b + return k } type cobraFunc func(cmd *cobra.Command, args []string) diff --git a/settings/settings.go b/settings/settings.go index e985335a..0d485b77 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -1,6 +1,7 @@ package settings import ( + "crypto/rand" "strings" "github.com/filebrowser/filebrowser/v2/rules" @@ -41,3 +42,15 @@ type Server struct { func (s *Server) Clean() { s.BaseURL = strings.TrimSuffix(s.BaseURL, "/") } + +// GenerateKey generates a key of 256 bits. +func GenerateKey() ([]byte, error) { + b := make([]byte, 64) + _, err := rand.Read(b) + // Note that err == nil only if we read len(b) bytes. + if err != nil { + return nil, err + } + + return b, nil +}