[Free] Random Password Generator Extension by ChatGPT

I asking for ChatGPT to create Random Password Generator extension for AI2, Mit App Inventor type of similar site and the result is insane.

The features

  • Customizable password Length
  • Special character
  • Capital Latter
  • Number

Source code

package com.yourcompany.passwordgenerator;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.*;
import java.util.Random;

@DesignerComponent(
        version = 1,
        description = "Extension for Customizable Password Generator",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "https://image.flaticon.com/icons/png/512/632/632351.png")

@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")

public class PasswordGenerator extends AndroidNonvisibleComponent {

    //Activity and Context
    private Context context;
    private Activity activity;

    public PasswordGenerator(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }

    @SimpleFunction(description = "Generates a random password with customizable options")
    public String GeneratePassword(int passwordLength, int numSpecialChars, int numCapitalLetters, int numNumbers) {

        // Initialize character sets
        String specialChars = "!@#$%^&*()_+-=[]{}|;':\",./<>?";
        String capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String smallLetters = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "0123456789";

        // Create a string builder to construct the password
        StringBuilder password = new StringBuilder();

        // Set up a random generator
        Random random = new Random();

        // Generate the password with the specified criteria
        for (int i = 0; i < passwordLength; i++) {
            // Decide which character set to choose from randomly
            int charSetIndex = random.nextInt(4);

            // Determine which set to use
            String charSet = "";
            switch (charSetIndex) {
                case 0:
                    charSet = specialChars;
                    break;
                case 1:
                    charSet = capitalLetters;
                    break;
                case 2:
                    charSet = smallLetters;
                    break;
                case 3:
                    charSet = numbers;
                    break;
            }

            // Generate a random character from the chosen set
            int charIndex = random.nextInt(charSet.length());
            char randomChar = charSet.charAt(charIndex);

            // Add the character to the password builder
            password.append(randomChar);

            // Count the number of special characters, capital letters, and numbers in the password
            int specialCharCount = 0;
            int capitalLetterCount = 0;
            int numberCount = 0;

            for (int j = 0; j < password.length(); j++) {
                char c = password.charAt(j);
                if (specialChars.indexOf(c) != -1) {
                    specialCharCount++;
                } else if (capitalLetters.indexOf(c) != -1) {
                    capitalLetterCount++;
                } else if (numbers.indexOf(c) != -1) {
                    numberCount++;
                }
            }

            // Check if the criteria for special characters, capital letters, and numbers has been met
            if (specialCharCount == numSpecialChars && capitalLetterCount == numCapitalLetters && numberCount == numNumbers) {
                break;
            }
        }

        // Return the generated password
        return password.toString();
    }
}

Note: I think there was something mistake with the code I hope a expert extension developer spot the mistake and fix it. By the way the extension still usable.

Download the extension
PasswordGenerator.aix (6.4 KB)

3 Likes

Usefull extension Great job. :sparkling_heart: :boom: :boom: :boom:

1 Like