feat: export generate key

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
This commit is contained in:
Henrique Dias 2019-01-11 20:25:39 +00:00
parent 6dbf801370
commit a3daac84a2
5 changed files with 20 additions and 9 deletions

View File

@ -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{}

View File

@ -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,

View File

@ -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: ".",

View File

@ -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)

View File

@ -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
}