Skip to content

Instantly share code, notes, and snippets.

@mpgn
Last active March 18, 2016 10:35
Show Gist options
  • Save mpgn/c6fd887a94837e3bbac0 to your computer and use it in GitHub Desktop.
Save mpgn/c6fd887a94837e3bbac0 to your computer and use it in GitHub Desktop.
brute force hash (md5, sha1) first char in Go using combinaison from Python or rand
package main
import (
"fmt"
"os"
"crypto/md5"
//"crypto/sha1"
//"math/rand"
"time"
"encoding/hex"
"strings"
"strconv"
"bytes"
)
var start = time.Now()
var prefix = os.Args[1]
var length = os.Args[2]
func combinations(iterable []int, r int) {
pool := iterable
n := len(pool)
if r > n {
return
}
indices := make([]int, r)
for i := range indices {
indices[i] = i
}
result := make([]int, r)
for i, el := range indices {
result[i] = pool[el]
}
//fmt.Println(result)
for {
i := r - 1
for ; i >= 0 && indices[i] == i+n-r; i -= 1 {
}
if i < 0 {
return
}
indices[i] += 1
for j := i + 1; j < r; j += 1 {
indices[j] = indices[j-1] + 1
}
for ; i < len(indices); i += 1 {
result[i] = pool[indices[i]]
}
var buffer bytes.Buffer
for i := 0; i < len(result); i++ {
fmt.Printf("combinations %s\n",attemp)
buffer.WriteString(string(result[i]))
}
res := pown(buffer.String())
if res {
break
}
}
}
func pown(attemp string) bool {
hash := md5.New()
hash.Write([]byte(attemp))
hashString := hex.EncodeToString(hash.Sum(nil))
//compare the cmdline input with the md5 prefix
if strings.HasPrefix(hashString,prefix){
fmt.Printf("combinations %s\n",attemp)
return true
}
return false
}
func main() {
// add some randomness
//rand.Seed(time.Now().UnixNano())
l, err := strconv.Atoi(length)
if err != nil {
// handle error
fmt.Println(err)
os.Exit(2)
}
var charset = []int{97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 61, 47, 43}
combinations(charset, l)
elapsed := time.Since(start)
fmt.Println(elapsed)
}
package main
import (
"fmt"
"os"
"crypto/md5"
//"crypto/sha1"
"math/rand"
"time"
"encoding/hex"
"strings"
"strconv"
)
var start = time.Now()
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=/+")
//var letters = []rune("!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func pown(nonce string, n int) {
//generate md5 from random strings
for {
attemp := randSeq(n)
//hash := sha1.New()
hash := md5.New()
hash.Write([]byte(attemp))
hashString := hex.EncodeToString(hash.Sum(nil))
//compare the cmdline input with the md5 nonce
if strings.HasPrefix(hashString,nonce){
fmt.Printf("random %s\n",attemp)
break
}
}
}
func main() {
// ad some randomness
rand.Seed(time.Now().UnixNano())
//argument from cmdline
prefix := os.Args[1]
length := os.Args[2]
n, err := strconv.Atoi(length);
if err != nil {
// Invalid string
}
pown(prefix,n)
elapsed := time.Since(start)
fmt.Println(elapsed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment