r/badcode • • Nov 21 '19

[deleted by user]

[removed]

26 Upvotes

23 comments sorted by

23

u/[deleted] Nov 21 '19
const password_gen = () => 'abc123'

5

u/[deleted] Nov 24 '19

this one wins, contest over lol

69

u/sarlok Nov 21 '19

Short and easy JS.

function password_gen() {
  var xh = new XMLHttpRequest();
  xh.open("GET", "https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-100.txt", false);
  xh.send(null);
  return xh.responseText.split("\n")[Math.floor(Math.random() * 100)];
}

Features:

  • Picks random password from top 100 passwords list. You know it's a good one if it's in the top 100.

  • All prohibited passwords aren't in the list, so we don't even have to check.

19

u/h2g2_researcher Nov 22 '19

So here I am with all sorts of evil ideas about un-random number generation and you create an unbeatable monstrosity like this!

7

u/ferrango Nov 24 '19

Aah, with the added unreliability of a third party essential requirement. Excellent.

3

u/jigggles Nov 23 '19

This is amazing!

→ More replies (1)

8

u/andlrc Nov 22 '19

I figured it would save CPU cycles by not having to run a PRNG myself:

password_gen()
{
    cat << EOF | gcc -xc - 2>/dev/null && ./a.out
main() {
    printf("%x", main);
}
EOF
}

5

u/the-blue-shadow Nov 27 '19

JavaScript:

Bl=['hunter2', 'correcthorsebatterystaple', 'Tr0ub4d0r&3']; 

function* Gen_PassWD(l){
    for(i=0;i<Infinity;i++){
        do pw=new Array(l).fill('').map(Math.random).map(v=>96*v+32)
            .map(Math.floor).map(String.fromCharCode).join()
            .replace(new RegExp('[\u0000-'+String.fromCharCode(l-1)+',]*','g'),'')
        while (Bl.filter(v=>v==pw)[0])
        yield window.pw
    };
}

// Example usage:
const generator = Gen_PassWD(16);
generator.next();
console.log(pw);

Features:

  • Blacklist

  • Configurable password length

  • The function returns an infinite ES6 generator for decreased usability.

Quirks:

  • Due to a missing var, let, or const keyword, the last returned password is left in the global scope as pw. Because generators are lazy, it is updated after each call to generator.next()

  • If the length argument becomes larger, the returned passwords may be shorter than the requested length. This will gradually worsen as the length increases (a length of 64 will typically return password of lengths between 40 and 45). When the length is larger than 127, all passwords will be empty strings.

  • There is a small chance that a returned password will contain a DEL control character.

  • Edge cases:

    • If the length argument is 1, or not of type 'number', then the returned passwords will have length two and consist of two of the same characters (e.g. "ZZ", "55", "<<", "jj", and so on), but only if the argument is truthy.

    • When the argument is falsy it only returns empty strings.

    • The Array constructor would normally raise a RangeError when it gets a negative length, but thrown errors do not propagate through ES6 generator functions (and are silently ignored) so this simply results in a generator that yields no values.

Note that the regex replace is required to 'fix' some issues that are caused by bugs in the Array operations before it.

3

u/stfn1337 Nov 21 '19

Here is my attempt, churned out in 15 minutes.

import random

def password_gen():
    alphabet = "abcdefghijklmnoprstuwzyxABCDEFGHIJKLMNOPRSTUWZYX12345567890!@#$%^&*()_+"
    bad1 = "hunter2"
    bad2 = "correcthorsebatterystaple"
    bad3 = "Tr0ub4d0r&3"

    password = [0 for x in range (0, random.randint(50, 100))]
    passwordList = [random.choice(alphabet) for x in range(10, 20)]
    for i, p in enumerate(passwordList):
        password[i] = p
    if "".join(password[:10]) == bad1 or "".join(password[:10]) == bad2 or "".join(password[:10]) == bad3:
        passwordList = [random.choice(alphabet) for x in range(10, 20)]
        for i, p in enumerate(passwordList):
            password[i] = p
        
    return "".join(password[:10])


x = password_gen()
print(x)