[SOLVED] Go loops use 100% memory and crash windows

Hello ! ( i’m french )

I’m trying to create a bruteforce password software. This is a very good exercise

How i can clear the memory or limit the memory usage ?

package main

import (
  "fmt"
  "math"
  "math/rand"
  "crypto/sha1"
  "bytes"
  "io"
  "runtime"  
  "time"
)

// characters used for conversion
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

// converts number to base62
func Encode(number int) string {
  if number == 0 {
    return string(alphabet[0])
  }

  chars := make([]byte, 0)

  length := len(alphabet)

  for number > 0 {
    result    := number / length
    remainder := number % length
    chars   = append(chars, alphabet[remainder])
    number  = result
  }

  for i, j := 0, len(chars) - 1; i < j; i, j = i + 1, j - 1 {
    chars[i], chars[j] = chars[j], chars[i]
  }

  return string(chars)
}

// converts base62 token to int
func Decode(token string) int {
  number := 0
  idx    := 0.0
  chars  := []byte(alphabet)

  charsLength := float64(len(chars))
  tokenLength := float64(len(token))

  for _, c := range []byte(token) {
    power := tokenLength - (idx + 1)
    index := bytes.IndexByte(chars, c)
    number += index * int(math.Pow(charsLength, power))
    idx++
  }

  return number
}

func TestPassword(token int) string {
  password := Encode(token)
  bases++
  return password
}

var bases = 0;
var password_cible = "a05bd890c4868ea1807f8564055d1fba77c6ba81"

func hash(){

  password := TestPassword(bases)
  h := sha1.New()
  io.WriteString(h, password)
  password_hash := fmt.Sprintf("%x", h.Sum(nil))
  //fmt.Println(password_hash , " | ", bases, " | ", password)

  if password_hash != password_cible {
    hash()
  } else {
    fmt.Println(password_hash, " PASSWORD CRACKED ", password)
  }

}


func main(){
  hash()
}

The memory ram increases more and more and crashes windows

Thx !!! :slight_smile:

Hi,

hash() is recursive. Each time it is called, a new stack frame is created. Stack frames take up memory.

In this case, hash() is called 5,120,963 times (this is actually the value of bases when complete).

To limit the amount of memory used, you need a non-recursive version of hash(). Here is one that I kept as close to what you wrote as I could:

func hash() {
	for {
		password := TestPassword(bases)
		h := sha1.New()
		io.WriteString(h, password)
		password_hash := fmt.Sprintf("%x", h.Sum(nil))
		//fmt.Println(password_hash , " | ", bases, " | ", password)

		if password_hash == password_cible {
			fmt.Println(password_hash, " PASSWORD CRACKED ", password)
			return
		}
	}
}

With this hash function, the program completed in 13.6 seconds on my computer.

Hope this helps.

4 Likes

Wow !

1000 thanks to you! I do not have that logic of operation. I have been working with nodejs for years doing the job all by myself.

I will try again with different projects to see if I understood the funciton.

Thx !!!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.