main.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package main import ( "crypto/rand" "encoding/base64" "fmt" "log" "net/http" "runtime" "github.com/google/uuid" "golang.org/x/crypto/argon2" ) func main() { // Ensure we use all available cores for maximum performance numCPU := runtime.NumCPU() runtime.GOMAXPROCS(numCPU) log.Printf("Server starting with %d CPU cores available", numCPU) // Setup routes http.HandleFunc("/", benchmarkHandler) // Start server log.Println("Benchmark API server listening on :5001") if err := http.ListenAndServe(":5001", nil); err != nil { log.Fatalf("Server failed to start: %v", err) } } func benchmarkHandler(w http.ResponseWriter, r *http.Request) { // Generate a new UUID for this request id := uuid.New() // Generate a random salt salt := make([]byte, 16) if _, err := rand.Read(salt); err != nil { http.Error(w, "Failed to generate salt", http.StatusInternalServerError) return } // Run expensive Argon2id hashing operation // Parameters chosen to be computationally expensive // - time: 3 iterations (higher values increase computation time) // - memory: 64 MB (higher values increase memory usage) // - threads: 4 (adjust based on testing, too high might cause contention) // - key length: 32 bytes hash := argon2.IDKey([]byte(id.String()), salt, 3, 64*1024, 4, 32) hashStr := base64.StdEncoding.EncodeToString(hash) // Return the result w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, `{"uuid":"%s","hash":"%s"}`, id, hashStr) } |