1

I'm having a strange issue.

I have a package named tools in which I have various files with tools for my main package, one of them generates a pseudorandom string that should contain uppercase, lowercase, numerical and certain special characters, to make sure I don't get a string that misses some of the types I did some validations and yet, i seem to miss something because I get an error every now and then

This is my main file:

package main

import (
    "../tools"
    "fmt"
    "strings"
)

const lower = "abcdefghizklmnopqrstuvwxyz"
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numrical= "0123456789"
const special = "!#$^*"

func main (){

    for i :=0; i<10; i++ {
        str := tools.GenSpecial(15)
        fmt.Println(str, validate(str))
    }

}

func haslower (s string) bool {
    return strings.ContainsAny(s,lower)
}

func hasupper (s string) bool {
    return strings.ContainsAny(s,upper)
}

func hasnumrical (s string) bool {
    return strings.ContainsAny(s,numrical)
}

func hasspecial (s string) bool {
    return strings.ContainsAny(s,special)
}

func validate (s string) bool {
    return haslower(s) && hasupper(s) && hasnumrical(s) && hasspecial(s)
}

and this is the relevant parts from my tools file:

package tools

import (
    "math/rand"
    "time"
    "strings"
)
const alphanum = 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const specialchars = 
"abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$^*"
const lower = "abcdefghizklmnopqrstuvwxyz"
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numrical= "0123456789"
const special = "!#$^*"

func randomize() {
    rand.Seed(time.Now().UnixNano())
}
func GenSpecial(n int) string { //function to generate a psuedorandom 
alphabetical string with special characters
    rstring := make([]byte, n)
    for i := range rstring {
        randomize()
        rstring[i] = specialchars[rand.Intn(len(specialchars))]
    }
    if validate(string(rstring))&& string(rstring)!=""{
        return string(rstring)
    } else {
        GenSpecial(n)
    }
    return "abc"
}
func haslower (s string) bool {
    return strings.ContainsAny(s,lower)
}

func hasupper (s string) bool {
    return strings.ContainsAny(s,upper)
}

func hasnumrical (s string) bool {
    return strings.ContainsAny(s,numrical)
}

func hasspecial (s string) bool {
    return strings.ContainsAny(s,special)
}

func validate (s string) bool {
    return haslower(s) && hasupper(s) && hasnumrical(s) && hasspecial(s)
}

When I run my main file, i get some values that return the "abc" value, and I don't understand how or why.

Any ideas?

2
  • 2
    Do not use relative paths in imports. You should only seed the PRNG once. Commented Oct 23, 2017 at 15:13
  • 1
    You don't need to reseed the rand package every loop. Just seed it once at the start of the program. Commented Oct 23, 2017 at 15:13

1 Answer 1

7

You are missing a return statement in your else case. If validate returns false, you call GenSpecial and then it returns "abc".

You want to say:

if validate(string(rstring))&& string(rstring)!=""{
    return string(rstring)
} else {
    return GenSpecial(n) // return here!
}
Sign up to request clarification or add additional context in comments.

1 Comment

I feel kinda stupid that I forgot the return in there, but nice catch, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.