Skip to content

Commit

Permalink
chore: split out handler, added comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nronzel committed Feb 10, 2024
1 parent 226d0d3 commit 2390bba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
4 changes: 3 additions & 1 deletion pkg/decryption/decryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 0 additions & 15 deletions pkg/handlers/handlerDecrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

dc "github.com/nronzel/xoracle/pkg/decryption"
tmpls "github.com/nronzel/xoracle/templates"
"github.com/nronzel/xoracle/utils"
)

Expand Down Expand Up @@ -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)
}
}
22 changes: 22 additions & 0 deletions pkg/handlers/handlerRoot.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 2390bba

Please sign in to comment.