'Why is this script hanging on command prompt?

I have the following script that generates random sequences and hashes them on the cpu with several threads in go.

package main

import(
    "fmt"
    "crypto/sha256"
    "encoding/hex"
    "math/rand"
    "time"
    "log"
    "os"
)

func randChr(i int)(string){
    i = i + 65
    if i>90 {
        i = i - 43
    }
    return string(i)
}

func randStr(random *rand.Rand, length int)(string){
    result := ""
    for len(result)<length{
        result = result + randChr(random.Intn(36))
    }
    return result
}

func HashPass(data []byte) (bool,[32]byte){
    hash := sha256.Sum256(data)
    s := hex.EncodeToString(hash[:])
    pass := true
    for i := 0; i<7; i++ {
        if s[i] != s[i+1]{
            pass = false
            break;
        }
    }
    return pass,hash
}


func getAPassingHash()(string){
    randSource := rand.NewSource(time.Now().UnixNano())
    random := rand.New(randSource)
    passes := false
    s := ""
    for !passes {
        s=randStr(random,64)
        passes,_ =  HashPass([]byte(s))
    }
    return(s)
}

func worker(ch chan string){
    ch <- getAPassingHash()
}

func timer(ch chan string,wait time.Duration){
    time.Sleep(wait)
    ch <- "End"
}

func append(fn string,conts string){
    f, err := os.OpenFile(fn,
        os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    if err != nil {
        log.Println(err)
    }
    defer f.Close()
    if _, err := f.WriteString(conts); err != nil {
        log.Println(err)
    }
}

func main(){
    ch := make(chan string)
    go timer(ch,6*time.Hour)
    for i:=0;i<9;i++{
        time.Sleep(time.Second)
        go worker(ch)
    }
    for true{
        result := <-ch
        if result == "End"{
            break;
        }
        go worker(ch)
        fmt.Println(result)
        hash := sha256.Sum256([]byte(result))
        fmt.Println(hex.EncodeToString(hash[:]))
        fmt.Println()
        append("hashes.txt","\n"+result+"\n"+hex.EncodeToString(hash[:])+"\n")
    }
    fmt.Println("done")
}

For some reason the script hangs up occasionally until I click on the command prompt and hit enter. I'm not sure where it is getting stuck but I can see that my system CPU utilization goes down and I know I have results somehow blocking out the program, I hit enter and results print and cpu usage spikes back up. I know this might be hard to replicate but I would really appreciate any suggestions.



Solution 1:[1]

It doesn't hang, it waits for the value of passes to be true

func getAPassingHash()(string){
    randSource := rand.NewSource(time.Now().UnixNano())
    random := rand.New(randSource)
    passes := false
    s := ""
    for !passes { // wait for value is true
        s=randStr(random,64)
        passes,_ =  HashPass([]byte(s))
    }
    return(s)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rahmat Fathoni