From ecde8b1d51a97d4d1f13206c75bf54c7d68e4f6e Mon Sep 17 00:00:00 2001 From: Anindya Maiti Date: Thu, 1 Feb 2024 10:57:55 -0600 Subject: [PATCH] Week3 --- hash-puzzle/hashing-puzzle.go | 102 ++++++++++++++++++++++++++++++++++ hashing/hashing-examples.go | 4 +- rsa/rsa-encrypt-sign.go | 12 ++-- 3 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 hash-puzzle/hashing-puzzle.go diff --git a/hash-puzzle/hashing-puzzle.go b/hash-puzzle/hashing-puzzle.go new file mode 100644 index 0000000..8330358 --- /dev/null +++ b/hash-puzzle/hashing-puzzle.go @@ -0,0 +1,102 @@ +package main + +import ( + "crypto/sha256" + "encoding/hex" + "fmt" + "math/rand" + "sort" + "time" +) + +func main() { + + trialset := []int{} + numtrials := 100 + for i := 0; i < numtrials; i++ { + trialset = append(trialset, hashTrial()) + } + + fmt.Println("Mean Trials: " + fmt.Sprint(mean(trialset))) + fmt.Println() + fmt.Println("Median Trials: " + fmt.Sprint(median(trialset))) + fmt.Println() + +} + +func median(numbers []int) float64 { + sort.Ints(numbers) + n := len(numbers) + if n%2 == 0 { + return float64(numbers[n/2-1]+numbers[n/2]) / 2 + } + return float64(numbers[n/2]) +} + +func mean(numbers []int) float64 { + var sum int + for _, n := range numbers { + sum += n + } + return float64(sum) / float64(len(numbers)) +} + +func hashTrial() int { + + // puzzle difficulty + difficulty := 5 + + // string to match leading zeros + m := "" + for i := 0; i < difficulty; i++ { + m += "0" + } + + // counter for number of trials + trials := 1 + + // original message to hash + s := "Hello" + + // random seed using time and generation of a nonce + rand.Seed(time.Now().UnixNano()) + nonce := rand.Intn(1000000) + + // compute first sha256 + h := sha256.New() + p := string(s) + " " + fmt.Sprint(nonce) + + h.Write([]byte(p)) + + fmt.Println() + fmt.Println(s) + fmt.Println() + fmt.Println(p) + fmt.Println() + fmt.Println(hex.EncodeToString(h.Sum(nil))) + fmt.Println() + + // compute sha256 until leading zeros are found + for string(hex.EncodeToString(h.Sum(nil))[:difficulty]) != m { + + h.Reset() + + nonce++ + p = string(s) + " " + fmt.Sprint(nonce) + h.Write([]byte(p)) + + fmt.Println(p) + fmt.Println() + fmt.Println(hex.EncodeToString(h.Sum(nil))) + fmt.Println() + + trials++ + + } + + fmt.Println("Trials: " + fmt.Sprint(trials)) + fmt.Println() + + return trials + +} diff --git a/hashing/hashing-examples.go b/hashing/hashing-examples.go index 7dd3667..509c638 100644 --- a/hashing/hashing-examples.go +++ b/hashing/hashing-examples.go @@ -7,8 +7,8 @@ import ( ) func main() { - s := "Hello World" - //s := "In the heart of a bustling city, a small, unassuming cafe stood tucked between towering skyscrapers. Its walls, a mosaic of colorful tiles, held stories of a thousand meetings, whispered secrets, and shared laughter. Inside, the aroma of freshly ground coffee mingled with the scent of old books, creating an ambiance of warmth and nostalgia. Each table bore the marks of countless cups and conversations, and the worn-out sofa in the corner was a silent testament to many a weary traveler finding solace in its embrace. Outside, the city rushed by in a blur of lights and sounds, but within the cafe's walls, time seemed to slow down, inviting passersby to pause and savor the small joys of life. ~ChatGPT" + //s := "Hello World" + s := "In the heart of a bustling city, a small, unassuming cafe stood tucked between towering skyscrapers. Its walls, a mosaic of colorful tiles, held stories of a thousand meetings, whispered secrets, and shared laughter. Inside, the aroma of freshly ground coffee mingled with the scent of old books, creating an ambiance of warmth and nostalgia. Each table bore the marks of countless cups and conversations, and the worn-out sofa in the corner was a silent testament to many a weary traveler finding solace in its embrace. Outside, the city rushed by in a blur of lights and sounds, but within the cafe's walls, time seemed to slow down, inviting passersby to pause and savor the small joys of life ~ChatGPT" sha256 := sha256.Sum256([]byte(s)) md5 := md5.Sum([]byte(s)) diff --git a/rsa/rsa-encrypt-sign.go b/rsa/rsa-encrypt-sign.go index 2a88954..27f2064 100644 --- a/rsa/rsa-encrypt-sign.go +++ b/rsa/rsa-encrypt-sign.go @@ -20,11 +20,11 @@ func main() { } alicePublicKey := &alicePrivateKey.PublicKey - trudyPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) - if err != nil { - fmt.Println(err) - os.Exit(1) - } + //trudyPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) + //if err != nil { + // fmt.Println(err) + // os.Exit(1) + //} //trudyPublicKey := &trudyPrivateKey.PublicKey bobPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) @@ -92,7 +92,7 @@ func main() { hashed := pssh.Sum(nil) signature, err := rsa.SignPSS( rand.Reader, - trudyPrivateKey, + alicePrivateKey, newhash, hashed, &opts)