Anindya Maiti 3 months ago
parent
commit
ecde8b1d51
  1. 102
      hash-puzzle/hashing-puzzle.go
  2. 4
      hashing/hashing-examples.go
  3. 12
      rsa/rsa-encrypt-sign.go

102
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
}

4
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))

12
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)

Loading…
Cancel
Save