Skip to content

Instantly share code, notes, and snippets.

@siliconcow
Last active January 4, 2016 05:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save siliconcow/8572979 to your computer and use it in GitHub Desktop.
Save siliconcow/8572979 to your computer and use it in GitHub Desktop.
Go gitcoin
package main
import (
"github.com/jgrahamc/go-openssl/sha1"
//"crypto/sha1"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"time"
//"sync"
"encoding/hex"
)
//func gitMoney(difficulty string,in []byte, w *sync.WaitGroup) {
func gitMoney(difficulty string,in []byte, w chan bool) {
hashes := 0
now := time.Now()
start_second := now.Truncate(time.Second)
i := 0
debug := true
for {
//text := fmt.Sprintf("tree %s \n parent %s \n author CTF user <me@example.com> %s +0000 \n committer CTF user <me@example.com> %s +0000 \n Give me a Gitcoin\n $d", "tree", "parent", "time", counter)
//dumb
t := strconv.Itoa(i)
counter := []byte(t)
h := sha1.New()
body := append(in, counter...)
fmt.Fprintf(h, "commit %d\x00", len(body))
h.Write(body)
sum := h.Sum(nil)
//cs := fmt.Sprintf("%x\n", h.Sum(nil))
cs := hex.EncodeToString(sum[:])
if cs < difficulty {
fmt.Printf("%s%s",in,t)
fmt.Fprintln(os.Stderr, cs)
break
}
hashes++
now := time.Now()
end_second := now.Truncate(time.Second)
if debug == true {
if end_second.After(start_second) {
fmt.Fprintln(os.Stderr, "hashes per second:", hashes)
start_second = end_second
hashes = 0
}
}
i++
}
w <- true
}
func main() {
in, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Println(err, string(in))
}
difficulty := os.Args[1]
fmt.Fprintln(os.Stderr, "Called with difficulty:", difficulty)
quit :=make(chan bool)
// var wg sync.WaitGroup
// wg.Add(1)
go gitMoney(difficulty, in, quit)
for {
select {
case <- quit:
return
}
}
// wg.Wait()
}
@siliconcow
Copy link
Author

proof:
(lib)root@awesomevm:/projects/level1# echo "testing" | go run dumb.go
038d718da6a1ebbc6a7780a96ed75a70cc2ad6e2
(lib)root@awesomevm:
/projects/level1# echo "testing" | git hash-object --stdin
038d718da6a1ebbc6a7780a96ed75a70cc2ad6e2

@siliconcow
Copy link
Author

now with crappy performing solver

@evandrix
Copy link

Line 30 should be "commit %d\x00", right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment