github.com/tommi2day/gomodules/pwlib@v0.0.0-20230217211148-82cdbcf0a79d/pwlib_config.go (about)

     1  package pwlib
     2  
     3  import (
     4  	"github.com/Luzifer/go-openssl/v4"
     5  	"os"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  const (
    11  	defaultRsaKeySize = 2048
    12  	typeGO            = "go"
    13  	typeOpenssl       = "openssl"
    14  	defaultMethod     = typeGO
    15  )
    16  
    17  // PassConfig Type for encryption configuration
    18  type PassConfig struct {
    19  	AppName         string
    20  	DataDir         string
    21  	KeyDir          string
    22  	KeyPass         string
    23  	CryptedFile     string
    24  	PrivateKeyFile  string
    25  	PubKeyFile      string
    26  	PlainTextFile   string
    27  	SessionPassFile string
    28  	Method          string
    29  	KeySize         int
    30  	SSLDigest       openssl.CredsGenerator
    31  }
    32  
    33  var label = []byte("")
    34  
    35  // PwConfig Encryption configuration
    36  var PwConfig PassConfig
    37  
    38  // SSLDigest variable helds common digist algor
    39  var SSLDigest = openssl.BytesToKeySHA256
    40  
    41  // SetConfig set encryption configuration
    42  func SetConfig(appname string, datadir string, keydir string, keypass string, method string) {
    43  	log.Debug("SetConfig entered")
    44  	log.Debugf("A:%s, P:%s, D:%s, K:%s, M:%s", appname, keypass, datadir, keydir, method)
    45  	// default names
    46  	ext := "gp"
    47  	wd, _ := os.Getwd()
    48  	etc := wd + "/etc"
    49  	if datadir == "" {
    50  		datadir = etc
    51  	}
    52  	if keydir == "" {
    53  		keydir = etc
    54  	}
    55  	if keypass == "" {
    56  		keypass = appname
    57  	}
    58  	if method == "" {
    59  		method = defaultMethod
    60  	}
    61  	if method == typeOpenssl {
    62  		ext = "pw"
    63  	} else if method == typeGO {
    64  		ext = "gp"
    65  	} else {
    66  		log.Warnf("invalid method %s, use method %s", method, defaultMethod)
    67  		method = defaultMethod
    68  		ext = "gp"
    69  	}
    70  	cryptedfile := datadir + "/" + appname + "." + ext
    71  	privatekeyfile := keydir + "/" + appname + ".pem"
    72  	pubkeyfile := keydir + "/" + appname + ".pub"
    73  	plainfile := datadir + "/" + appname + ".plain"
    74  	sessionpassfile := keydir + "/" + appname + ".dat"
    75  
    76  	// set global configuration defaults, any part can be overwritten
    77  	PwConfig.AppName = appname
    78  	PwConfig.DataDir = datadir
    79  	PwConfig.KeyDir = keydir
    80  	PwConfig.KeyPass = keypass
    81  	PwConfig.CryptedFile = cryptedfile
    82  	PwConfig.PrivateKeyFile = privatekeyfile
    83  	PwConfig.PubKeyFile = pubkeyfile
    84  	PwConfig.PlainTextFile = plainfile
    85  	PwConfig.SessionPassFile = sessionpassfile
    86  	PwConfig.Method = method
    87  	PwConfig.KeySize = defaultRsaKeySize
    88  	PwConfig.SSLDigest = SSLDigest
    89  }