Last active
January 4, 2016 05:09
-
-
Save siliconcow/8572979 to your computer and use it in GitHub Desktop.
Go gitcoin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
now with crappy performing solver
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
proof:
(lib)root@awesomevm:
/projects/level1# echo "testing" | go run dumb.go/projects/level1# echo "testing" | git hash-object --stdin038d718da6a1ebbc6a7780a96ed75a70cc2ad6e2
(lib)root@awesomevm:
038d718da6a1ebbc6a7780a96ed75a70cc2ad6e2