Skip to content

Instantly share code, notes, and snippets.

@robotmay
Created August 29, 2012 12:32
Show Gist options
  • Save robotmay/3511812 to your computer and use it in GitHub Desktop.
Save robotmay/3511812 to your computer and use it in GitHub Desktop.
Simple (and largely pointless) hash generator in Go
// Wrote this to try bruteforcing a hash key in part of the Stripe CTF game.
// I realise it would take something like 100 million years to find it, but I'd never written a hash generator before.
// Putting this on GitHub in case anyone's interested.
package main
import (
"math/rand"
"strings"
"bytes"
"fmt"
"crypto/sha1"
)
const Maxlen = 14
const Matching = "count=10&lat=37.351&user_id=1&long=-119.827&waffle=eggo"
const LookingFor = "7624a445bbb826611cd903770d8d42b414952090"
func generateKey(N int, channel chan string) {
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for {
r := make([]string, N)
for i := 0; i < N; i++ {
c := chars[rand.Intn(len(chars))]
r[i] = string(c)
}
str := strings.Join(r, "")
channel <- str
}
}
func main() {
channel := make(chan string)
go generateKey(14, channel)
for {
str := <-channel
fmt.Println(str)
// Join for hashing
var buffer bytes.Buffer
buffer.WriteString(str)
buffer.WriteString(Matching)
sha := sha1.New()
sha.Write([]byte(buffer.String()))
hash := fmt.Sprintf("%x", sha.Sum(nil))
fmt.Println(hash)
if hash == LookingFor {
fmt.Println("Found")
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment