This commit is contained in:
2026-07-24 12:16:29 -05:00
parent 57adcc5235
commit 19d42888fb
+29 -20
View File
@@ -3,19 +3,20 @@ package main
import ( import (
"crypto" "crypto"
_ "crypto/sha1" _ "crypto/sha1"
"encoding/hex" "encoding/base64"
"fmt" "fmt"
"math/rand" "math/rand"
"os"
"time" "time"
) )
func main() { func main() {
g := Generator{ g := Generator{
Difficulty: 26, Difficulty: 25,
} }
c := g.RandomChallenge() c := g.RandomChallenge()
c.Solve()
fmt.Println(c.String()) fmt.Println(c.String())
fmt.Println(c.Solve().String())
} }
type Challenge struct { type Challenge struct {
@@ -23,6 +24,7 @@ type Challenge struct {
Date [20]byte Date [20]byte
Difficulty byte Difficulty byte
Solution int64 Solution int64
bytes []byte
} }
type Generator struct { type Generator struct {
Difficulty byte Difficulty byte
@@ -63,17 +65,30 @@ var prefixLookup = []int{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
} }
func (c Challenge) Solve() Challenge { func (c *Challenge) Solve() {
bytes := make([]byte, 76) c.bytes = make([]byte, 76)
bytes = append(bytes, c.Input[:]...) c.bytes = append(c.bytes, c.Input[:]...)
bytes = append(bytes, c.Date[:]...) c.bytes = append(c.bytes, c.Date[:]...)
timer := time.Now()
for { for {
s := c.Validate()
if s {
fmt.Fprintf(os.Stderr, "%s: found solution %d in %dms\n", c.String(), c.Solution, time.Since(timer).Milliseconds())
break
}
if c.Solution > 0xffffffffffffff {
fmt.Fprintf(os.Stderr, "%s: could not find solution!", c.String())
}
c.Solution++
}
}
func (c *Challenge) CheckValue(v int64) bool {
for i := range 8 { for i := range 8 {
bytes[75-i] = byte(c.Solution >> (i * 8)) c.bytes[75-i] = byte(v >> (i * 8))
} }
sum := make([]byte, 20) sum := make([]byte, 20)
hash := crypto.SHA1.New() hash := crypto.SHA1.New()
hash.Write(bytes) hash.Write(c.bytes)
sum = hash.Sum(nil) sum = hash.Sum(nil)
count := 0 count := 0
for _, c := range sum { for _, c := range sum {
@@ -83,18 +98,12 @@ func (c Challenge) Solve() Challenge {
break break
} }
} }
if c.Solution%20000000 == 0 { return count >= int(c.Difficulty)
println(c.Solution)
} }
if count >= int(c.Difficulty) { func (c *Challenge) Validate() bool {
println(c.Solution) return c.CheckValue(c.Solution)
break
} }
c.Solution++ func (c *Challenge) String() string {
}
return c
}
func (c Challenge) String() string {
bytes := make([]byte, 0) bytes := make([]byte, 0)
bytes = append(bytes, c.Input[:]...) bytes = append(bytes, c.Input[:]...)
bytes = append(bytes, c.Date[:]...) bytes = append(bytes, c.Date[:]...)
@@ -103,5 +112,5 @@ func (c Challenge) String() string {
for i := range 7 { for i := range 7 {
bytes[75-i] = byte(c.Solution >> (i * 8)) bytes[75-i] = byte(c.Solution >> (i * 8))
} }
return hex.EncodeToString(bytes) return base64.URLEncoding.EncodeToString(bytes)
} }