Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save weltonrodrigo/4b2f050c8b7bfd4f2239f0fd8d475ac7 to your computer and use it in GitHub Desktop.
Save weltonrodrigo/4b2f050c8b7bfd4f2239f0fd8d475ac7 to your computer and use it in GitHub Desktop.
How can I generate Apple-like passwords directly in Google Sheets?

How can I generate Apple-like passwords directly in Google Sheets?

Yes, you absolutely can! You can leverage Google Apps Script to add custom functions to your Google Sheets, allowing you to create functions like the generateAppleLikePassword function provided. Here's how you can do it:

Steps:

  1. Open your Google Sheet: Go to Google Sheets and open the spreadsheet where you want to use this password generator.

  2. Open the Script Editor: In the Google Sheets menu, click on "Extensions" (or "Tools" in older versions) and then select "Apps Script". This will open a new tab with the Google Apps Script editor.

  3. Copy and Paste the Function: Copy the JavaScript code for the generateAppleLikePassword function provided below and paste it into the script editor. Replace the default function myFunction() { ... } code with this code.

    /**
     * Generates an Apple-like password using a provided seed, with randomized digit and uppercase positions in any segment.
     * Only one digit and one uppercase character will be present in the entire password.
     * The same seed always produces the same password structure with randomized positions.
     * The seed can be a CPF or any other string. If the seed string remains the same, the generated password will also be the same.
     *
     * @param {string|number} seed - A seed value (number or string) to generate the password.
     * @return {string} The generated password in the format xxxxxx-xxxxxx-xxxxxx.
     * @customfunction
     */
    function generateAppleLikePassword(seed) {
      if (seed === undefined) {
        throw new Error("Please provide a seed value.");
      }
    
      // If seed is a string, convert it to a numeric hash.
      if (typeof seed === 'string') {
        var hash = 0;
        for (var i = 0; i < seed.length; i++) {
          hash = seed.charCodeAt(i) + ((hash << 5) - hash);
        }
        seed = Math.abs(hash);
      } else {
        seed = Number(seed);
      }
    
      // Create a seeded random generator using a simple LCG (linear congruential generator)
      var m = 233280;
      var a = 9301;
      var c = 49297;
      var state = seed % m;
    
      function random() {
        state = (state * a + c) % m;
        return state / m;
      }
    
      function randomCharFrom(str) {
        var index = Math.floor(random() * str.length);
        return str.charAt(index);
      }
    
      // Define safe characters (confusing ones removed)
      var lower = "abcdefghjkmnopqrstwxyz";      // Removed "l" and "i", "v" and "u"
      var upper = "ABCDEFGHJKMNPQRSTWXYZ";         // Removed "I", "L" and "O", "V" and "U"
      var digits = "23456789";                        // Removed "0" and "1"
    
      // Generate segments with only lowercase letters initially
      var seg1 = "";
      for (var i = 0; i < 6; i++) {
        seg1 += randomCharFrom(lower);
      }
      var seg2 = "";
      for (var i = 0; i < 6; i++) {
        seg2 += randomCharFrom(lower);
      }
      var seg3 = "";
      for (var i = 0; i < 6; i++) {
        seg3 += randomCharFrom(lower);
      }
    
      var password = seg1 + seg2 + seg3; // Combine segments without hyphens for index manipulation
      var passwordWithHyphens = seg1 + "-" + seg2 + "-" + seg3; // Keep the hyphenated version for later
    
      // Generate a random digit and a random uppercase character
      var digit = randomCharFrom(digits);
      var upperChar = randomCharFrom(upper);
    
      // Generate two distinct random indices within the 18-character password
      var digitIndex = Math.floor(random() * 18);
      var upperIndex = Math.floor(random() * 18);
      while (upperIndex === digitIndex) { // Ensure indices are different
        upperIndex = Math.floor(random() * 18);
      }
    
      // Insert the digit and uppercase character at the random indices
      password = password.substring(0, digitIndex) + digit + password.substring(digitIndex + 1);
      password = password.substring(0, upperIndex) + upperChar + password.substring(upperIndex + 1);
    
      // Re-insert hyphens to format the password back to xxxxxx-xxxxxx-xxxxxx
      seg1 = password.substring(0, 6);
      seg2 = password.substring(6, 12);
      seg3 = password.substring(12, 18);
    
      return seg1 + "-" + seg2 + "-" + seg3;
    }
  4. Save the Script: Click the "Save" icon (floppy disk) in the script editor. You'll be prompted to name your script. Choose a name like "PasswordFunctions" or something similar and click "OK".

  5. Use the Function in your Sheet: Now, go back to your Google Sheet. You can use the generateAppleLikePassword function just like any other built-in Google Sheets function.

    • Provide a Seed: In any cell, you can type the formula =generateAppleLikePassword("my_seed") or =generateAppleLikePassword(A1) if cell A1 contains your seed value (which can be text like "my_seed", "cpf123456789", or a number).

    • Seed Matters: Important: The password generated is directly tied to the seed you provide. If you use the same seed, you will always get the same password. This is by design, as stated in the function's documentation. This is useful if you need to generate a consistent "Apple-like" password based on a specific identifier (like a username or account number).

Example:

If you put the seed "example" in cell A1, in cell B1 you would type:

=generateAppleLikePassword(A1)

Cell B1 will then display an Apple-like password generated using "example" as the seed.

Important Considerations:

  • Custom Function Tag: The /** @customfunction */ tag in the docstring is crucial. It tells Google Sheets to recognize generateAppleLikePassword as a custom function that can be used in your spreadsheets.
  • Seed Security: While this function generates passwords in an "Apple-like" format, remember that the security of the password ultimately depends on the seed and the algorithm itself. For highly sensitive applications, consider using more robust password generation methods.
  • Seed Persistence: Keep in mind that if you share your Google Sheet, anyone with access to the sheet (and the script) can potentially generate the same passwords if they know the seed.

Disclaimer:

This post was generated by an AI based on my own ideas about a nice password geneartor

Now you can easily create Apple-like passwords directly within your Google Sheets!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment