From 2390bba2e75265792db40c9f0c38ee5d21b66179 Mon Sep 17 00:00:00 2001 From: nronzel <86695181+nronzel@users.noreply.github.com> Date: Sat, 10 Feb 2024 13:35:02 -0500 Subject: [PATCH] chore: split out handler, added comment --- pkg/decryption/decryption.go | 4 +++- pkg/handlers/handlerDecrypt.go | 15 --------------- pkg/handlers/handlerRoot.go | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 pkg/handlers/handlerRoot.go diff --git a/pkg/decryption/decryption.go b/pkg/decryption/decryption.go index 97d5e37..920c09b 100644 --- a/pkg/decryption/decryption.go +++ b/pkg/decryption/decryption.go @@ -62,10 +62,12 @@ func GuessKeySizes(data []byte) ([]int, error) { // Loop through each possible key size from 2 to maxKeySize (inclusive). for keySize := 2; keySize <= maxKeySize; keySize++ { - // Calculate the average Hamming distance for this key size. + // Skip keySize if keySize*4 exceeds the length of the data. Not able + // to make a meaningful comparison. if keySize*4 > len(data) { continue } + // Calculate the average Hamming distance for this key size. averageDistance, err := averageHammingDistance(data, keySize) if err != nil { return nil, fmt.Errorf("calculating average distance: %w", err) diff --git a/pkg/handlers/handlerDecrypt.go b/pkg/handlers/handlerDecrypt.go index 8e2875b..d7c98e3 100644 --- a/pkg/handlers/handlerDecrypt.go +++ b/pkg/handlers/handlerDecrypt.go @@ -7,7 +7,6 @@ import ( "strings" dc "github.com/nronzel/xoracle/pkg/decryption" - tmpls "github.com/nronzel/xoracle/templates" "github.com/nronzel/xoracle/utils" ) @@ -71,17 +70,3 @@ func HandlerDecrypt(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, responseHTML.String()) } - -func HandlerRoot(w http.ResponseWriter, r *http.Request) { - tmpl, err := template.New("index").Parse(tmpls.IndexTemplate) - if err != nil { - w.Header().Set("Content-Type", "text-/plain") - http.Error(w, "failed to load template", http.StatusInternalServerError) - return - } - err = tmpl.Execute(w, nil) - if err != nil { - w.Header().Set("Content-Type", "text-/plain") - http.Error(w, "problem executing template", http.StatusInternalServerError) - } -} diff --git a/pkg/handlers/handlerRoot.go b/pkg/handlers/handlerRoot.go new file mode 100644 index 0000000..d40f8aa --- /dev/null +++ b/pkg/handlers/handlerRoot.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "html/template" + "net/http" + + tmpls "github.com/nronzel/xoracle/templates" +) + +func HandlerRoot(w http.ResponseWriter, r *http.Request) { + tmpl, err := template.New("index").Parse(tmpls.IndexTemplate) + if err != nil { + w.Header().Set("Content-Type", "text-/plain") + http.Error(w, "failed to load template", http.StatusInternalServerError) + return + } + err = tmpl.Execute(w, nil) + if err != nil { + w.Header().Set("Content-Type", "text-/plain") + http.Error(w, "problem executing template", http.StatusInternalServerError) + } +}