Make me a password generator!

7

2

I am making a customizable password generator.

But I (for some reason) thought it'd be better to let you guys do it.


Umm.. then?

You only get one integer. That will include all the information about the customization.

The first 4 bits (1,2,4,8) will be used for choosing which charsets will be used.

Each bit corresponds to:

1: Lowercase alphabet(qwertyuiopasdfghjklzxcvbnm)

2: Uppercase alphabet(QWERTYUIOPASDFGHJKLZXCVBNM)

4: Numbers(0123456789)

8: Special chars(All printable characters except ones stated above and the space)

The length of the password will be shifted 4 bits. In other words, multiplied by 16.

Then generate the password with the charsets and the length.

Examples

175 (10*16 + 15 (1111)): a_U0!/h96%

Matthew Roh

Posted 2017-05-06T03:41:22.580

Reputation: 5 043

Question was closed 2017-05-06T16:07:28.353

9In other words, multiplied by 16. I don't think that's correct. Perhaps "floor division by 16"? – Conor O'Brien – 2017-05-06T03:42:59.000

2Does this mean newlines are allowed in the generated password? Seems a bit unusual for a password. – numbermaniac – 2017-05-06T05:06:21.443

2Randomly? All possible passwords equally likely? – Luis Mendo – 2017-05-06T11:03:25.880

Answers

7

Python 2, 217 203 bytes

from random import*
r=[10]+range(33,127)
r=[r[65:91],r[33:59],r[16:26],r[:16]+r[26:33]+r[59:65]+r[91:]]
x=input()
l=x>>4
print''.join(map(chr,sample(sum([k[1]for k in enumerate(r)if x&2**k[0]],[])*l,l)))

Ugh this is horribly long. The part I think could be golfed the most is the part to get the charsets; that part was a pain.

By the way, I am @HyperNeutrino; I'm just getting enough rep to chat. :P

-14 bytes thanks @ValueInk!
Bug fix thanks to @ovs!

HyperNeutrino

Posted 2017-05-06T03:41:22.580

Reputation: 26 575

Isn't from random import* shorter? Also is there anything in Python 3 that requires you to use it over Python 2? Python 2's print saves a byte, you can drop the int call around input, and range returns the list right away. – Value Ink – 2017-05-06T06:32:41.093

@ValueInk Right, thanks. -14 bytes total! :) – HyperNeutrino – 2017-05-06T06:54:55.517

Currently 9 is in special chars, fix that by replacing r[16:25] with r[16:26] and r[25:33] with r[26:33] – ovs – 2017-05-06T09:05:38.823

Oh wait I just thought you were a sockpuppet of HyperNeutrino's. – Matthew Roh – 2017-05-06T12:05:00.300

@SIGSEGV I am pretty much, except this account and that won't be transferring any rep between them. :P – HyperNeutrino – 2017-05-06T13:32:10.047

@ovs Thanks; I'll fix that. – HyperNeutrino – 2017-05-06T13:32:16.440

3

Stacked, 80 79 78 bytes

:2 tb _4 drop ascii'\' 'modI'toarr+match[''#`]map\keep''#`\16/~~:>$randin~2/"!

Try it online! Takes input from the top of the stack and leaves output on the top of the stack. TIO link includes test suite (thingy) for convenience.

Conor O'Brien

Posted 2017-05-06T03:41:22.580

Reputation: 36 228

3

Mathematica 175 Bytes

""<>RandomChoice[(j=Join)@@Pick[c~j~{Complement[(r=CharacterRange)[1,127],j@@(c=r@@#&/@{{97,122},{65,90},{48,57}})]},(i=IntegerDigits[#,2])[[-4;;]],1],i[[;;-5]]~FromDigits~2]&

Coded pretty much as explained in the question. Seems like rearranging the ASCII table was expensive.

Also, there is a bug in CharacterRange which doesn't allow it to accept 0 as an argument (at least in M10.2, which is what I have handy on my laptop), so to fix that would need another 7 bytes to include the ASCII null character.

Kelly Lowder

Posted 2017-05-06T03:41:22.580

Reputation: 3 225

If the error you're getting is "Arguments 0 and 127 of CharacterRange are not both strings of length 1 or integers", I'm getting that on 11.1.1 too. – numbermaniac – 2017-05-06T05:00:26.650

1yep,that's the one – Kelly Lowder – 2017-05-06T05:33:36.353

Infix notations can decrease a few bytes, and @@#&/@ is just @@@ – JungHwan Min – 2017-05-06T06:12:03.647

3

PHP, 127 113 bytes

Jörg helped my tired brain with 10-2 bytes and I found another 2+4.

for(;$i<$argn>>4;$c=chr(rand()))foreach([lower,upper,digit,punct]as$t=>$f)$argn>>$t&(ctype_.$f)($c)&&$i+=print$c;

breakdown

Run as pipe with php -nR '<code>'.

// loop while length not reached
for(;$i<$argn>>4;
    // pick random character
    $c=chr(rand())
)
    // loop through bits and functions
    foreach([lower,upper,digit,punct]as$t=>$f)
        // if test is positive, increment length counter and print character
        $argn>>$t&(ctype_.$f)($c)&&$i+=print$c;

Neat tricks:
The ctype functions return false on failure (resulting in $argn>>$t&0, so the bit is irrelevant);
true on success (resulting in $argn>>$t&1, i.e. a test on bit $t).
If that test fails, short circuit kicks in and the character is skipped.
If it is positive, $i will be increased by the return value of print (always 1).

Titus

Posted 2017-05-06T03:41:22.580

Reputation: 13 814

Why eval? $argn>>$t&(ctype_.$f)($c)&&print$c.!++$i; should do the same - 1 Byte for chr(rand()%127) instead of chr(rand(9,126)) – Jörg Hülsermann – 2017-05-06T10:49:39.730

1@JörgHülsermann Ah I couldn´t remember how to do that concatenation thing. Thanks! But I don´t need that modulo: cast to char implies 8 bit, and the unprintables still get kicked by ctype. – Titus – 2017-05-06T14:21:38.453

2

Japt, 93 78 bytes

Saved 15 bytes thanks to ETHproductions

;>>4 o_=ApU&1©CU&2©BU&4©9o U&8©Q+"\{|}\\!#$%&'()*+,-.:;<=>?@[]^_`" f q)gMqZlÃq

Try it online!

Luke

Posted 2017-05-06T03:41:22.580

Reputation: 4 675

Let's see... you can save several bytes by changing o r_+(Y=...gMqYl}P to o_=...gMqZl} q, another two by changing r+P to q, and some more by creating the range [0...9] instead of 165+"7890"+234. (You're missing a \{|} from the end of the string, btw) – ETHproductions – 2017-05-06T11:02:55.167

2

Python 2, 180 bytes

lambda x,r=[10]+range(33,127):''.join(chr(choice(r[65:91]*(x&1)+r[33:59]*(x&2>0)+r[16:26]*(x&4>0)+(r[:16]+r[26:33]+r[59:65]+r[91:])*(x&8>0)))for i in' '*(x>>4))
from random import*

Try it online!

ovs

Posted 2017-05-06T03:41:22.580

Reputation: 21 408

0

Jelly, 54 bytes

ØBUð,ø33r126µFỌQðṁø“ııµþ’b250Rðị@ø³%16b2UTµF
¢XȮµ:⁴’µ¡

Try it online!

Explanation

ØBUð,ø33r126µFỌQðṁø“ııµþ’b250Rðị@ø³%16b2UTµF  - nilad which returns charset
ØB                                            - yield alphanumeric characters
  U                                           - reverse to get proper order
   ð,ø33r126                                  - append char values of printable characters
            µFỌQ                              - flatten and remove repeats to get "z-aZ-A9-0[specials]"
                ðṁø                           - mold this string like ...
                   “ııµþ’b250                 - literal [26,26,10,32]
                             R                - [26R,26R,10R,32R]. All that matters is the length of each sublist.
                              ðị@             - get the elements numbered ...
                                 ³%16b2UT     - those indices we want based on the last 4 bits of the input.

¢XȮµ:⁴’µ¡ - monad with input a
 X        - pick a random element from ...
¢         - the above charset.
  Ȯ       - Print it.
        ¡ - Repeat this ...
    µ:⁴’  - floor(a/16)-1 times. (⁴ is literal 16).

fireflame241

Posted 2017-05-06T03:41:22.580

Reputation: 7 021