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?