From 30225e1e63dcd7123b2e23aace3945c26df1f686 Mon Sep 17 00:00:00 2001 From: Yad Smood <1415488+ysmood@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:08:47 +0800 Subject: [PATCH] support WHISPER_DTM_KEY --- constants.go | 6 ++++-- main.go | 33 ++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/constants.go b/constants.go index fdc3379..c23f428 100644 --- a/constants.go +++ b/constants.go @@ -4,9 +4,11 @@ import ( "os" ) -var WHISPER_DEFAULT_KEY_PATH = os.Getenv("WHISPER_DEFAULT_KEY_PATH") +var WHISPER_KEY_PATH = os.Getenv("WHISPER_KEY_PATH") -var WHISPER_DEFAULT_KEY = os.Getenv("WHISPER_DEFAULT_KEY") +var WHISPER_KEY = os.Getenv("WHISPER_KEY") + +var WHISPER_DTM_KEY = os.Getenv("WHISPER_DTM_KEY") var WHISPER_PASSPHRASE = os.Getenv("WHISPER_PASSPHRASE") diff --git a/main.go b/main.go index 4cc57a6..4821773 100644 --- a/main.go +++ b/main.go @@ -41,9 +41,10 @@ func main() { //nolint: funlen,gocyclo,cyclop WHISPER_AGENT_ADDR = WHISPER_AGENT_ADDR_DEFAULT } - privateKey := flags.String("p", WHISPER_DEFAULT_KEY_PATH, "Private key path to decrypt data.\n"+ - "Use env var WHISPER_DEFAULT_KEY to set the default key data.\n"+ - "Use env var WHISPER_DEFAULT_KEY_PATH to set the default key path.\n"+ + privateKey := flags.String("p", WHISPER_KEY_PATH, "Private key path to decrypt data.\n"+ + "Use env var WHISPER_KEY to set the default key data.\n"+ + "Use env var WHISPER_DTM_KEY to set the seed for the deterministic key date.\n"+ + "Use env var WHISPER_KEY_PATH to set the default key path.\n"+ "If it's empty a key in ~/.ssh will be auto selected.\n"+ "If it requires a passphrase, env var WHISPER_PASSPHRASE will be used or a password cli prompt will show up.\n"+ "The file path should always use / as the separator, even on Windows.") @@ -193,13 +194,27 @@ func getPrivate(decrypt bool, sign bool, location string, meta *whisper.Meta) *w return nil } - if location == "" && WHISPER_DEFAULT_KEY != "" { - private := whisper.PrivateKey{ - Data: []byte(WHISPER_DEFAULT_KEY), - Passphrase: WHISPER_PASSPHRASE, + if location == "" { + if WHISPER_DTM_KEY != "" { + key, _, err := secure.GenerateKeyFile(true, "", WHISPER_DTM_KEY) + if err != nil { + exit(err) + } + + return &whisper.PrivateKey{ + Data: key, + Passphrase: WHISPER_DTM_KEY, + } } - return ensurePassphrase(private, location) + if WHISPER_KEY != "" { + private := whisper.PrivateKey{ + Data: []byte(WHISPER_KEY), + Passphrase: WHISPER_PASSPHRASE, + } + + return ensurePassphrase(private, location) + } } if location == "" && decrypt { @@ -255,7 +270,7 @@ func findPrivateKey(meta *whisper.Meta) string { return p } - return WHISPER_DEFAULT_KEY_PATH + return WHISPER_KEY_PATH } func getPublicKeys(paths []string) []whisper.PublicKey {