Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,683 questions

55,435 answers

573 users

How to check whether a given password is strong, medium, or weak in Go

1 Answer

0 votes
// You can set your own rules

package main

import (
	"fmt"
	"unicode"
)

func checkPasswordStrength(password string) string {
	length := len(password)

	hasLower, hasUpper, hasDigit, specialChar := false, false, false, false

	lowuppdig := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

	for _, char := range password {
		if unicode.IsLower(char) {
			hasLower = true
		}
		if unicode.IsUpper(char) {
			hasUpper = true
		}
		if unicode.IsDigit(char) {
			hasDigit = true
		}
		if !contains(lowuppdig, char) {
			specialChar = true
		}
	}

	if hasLower && hasUpper && hasDigit && specialChar && length >= 10 {
		return "Strong"
	} else if (hasLower || hasUpper) && specialChar && length >= 8 {
		return "Medium"
	}

	return "Weak"
}

func contains(s string, char rune) bool {
	for _, c := range s {
		if c == char {
			return true
		}
	}
	return false
}

func main() {
	passwords := []string{"aq1o@p9$XM", "asW!W)(o", "WSDFK!#Q", "n*djskq*", "WE3q#$"}
	
	for _, password := range passwords {
		fmt.Println(checkPasswordStrength(password))
	}
}


/*
run:

Strong
Medium
Medium
Medium
Weak

*/

 



answered Oct 23, 2024 by avibootz
edited Oct 23, 2024 by avibootz
...