ID:195102
 
//Title: Random Password Generator
//Credit to: Jtgibson
//Contributed by: Jtgibson


//This is a powerful random password generator. It has options to generate
// passwords for your left hand, for your right hand, or for the full keyboard
// range. It also gives options to use exclusively lower-case, exclusively upper-
// case, or mixed case. Finally, it allows you to select from alphanumeric, alphabetic,
// numeric, or random character passwords.
//When using full range, random character, mixed case passwords, the passwords are
// practically unbreakable.

//Passwords generated are designed to use all fingers on the hand that it is selecting
// from. Thus, a password will never use the same finger twice. For example, if the
// letter was a "Q", then the next letter will not be a "Q", "A", or "Z". This is
// designed as a security feature. Specify "fingered=0" as the last argument to the
// GeneratePassword() proc if you don't mind using the same finger twice.

//Note: when generating a numeric password, the case is irrelevant.


//You could actually use this to create a game, where the players are in macro mode and
// each key press registers against a text string. You generate a random string, and the
// first player to type it all out wins the points there.

//This snippet is featured through an HTML form in The Haven Seed.


randpwd
//These lists are arranged in terms of fingers:
var/tmp/list/lowercase_keys = list(\
list("q","a","z"), //left pinky
list("w","s","x"), //left ring
list("e","d","c"), //left middle
list("r","f","v","t","g","b"), //left index
list("y","h","n","u","j","m"), //right index
list("i","k"), //right middle
list("o","l"), //right ring
list("p")) //right pinky
var/tmp/list/uppercase_keys = list(\
list("Q","A","Z"),
list("W","S","X"),
list("E","D","C"),
list("R","F","V","T","G","B"),
list("Y","H","N","U","J","M"),
list("I","K"),
list("O","L"),
list("P"))
var/tmp/list/numerals = list(\
list("1"),
list("2"),
list("3"),
list("4","5"),
list("6","7"),
list("8"),
list("9"),
list("0"))
var/tmp/list/special_chars = list(\
list("!"),
list("@"),
list("#"),
list("$","%"),
list("^","&"),
list("*"),
list("("),
list(")"))

proc/GeneratePassword(length=6, mixedcase=0,alpha=1,hands=-1,fingered=1)
//Mixedcase settings: -1 = uppercase only, 0 = lowercase only, 1 = either case
//Alpha settings: -1 = use alphabetic only, 0 = use numerals only, 1 = alphanumeric,
// 2 = use any character
//Hands settings: -1 = both, 1 = left, 2 = right
//Fingered: if false, the same finger can be used twice in a row

ASSERT(length > 0)
ASSERT(mixedcase <= 1 && mixedcase >= -1)
ASSERT(alpha <= 2 && alpha >= -1)
ASSERT(hands != 0 && hands <= 2 && hands >= -1)

var/string = ""


switch(hands)
if(-1)
var/fingers = list(1,2,3,4,5,6,7,8)
var/last_finger = pick(fingers)

while(length--)
var/finger = pick(fingered ? fingers - last_finger : fingers)

switch(mixedcase)
if(-1)
switch(alpha)
if(-1)
string += pick(uppercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + uppercase_keys[finger])
if(2)
string += pick(numerals[finger] + uppercase_keys[finger] + \
special_chars[finger])
if(0)
switch(alpha)
if(-1)
string += pick(lowercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + lowercase_keys[finger])
if(2)
string += pick(numerals[finger] + lowercase_keys[finger] + \
special_chars[finger])
if(1)
switch(alpha)
if(-1)
string += pick(uppercase_keys[finger] + lowercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + lowercase_keys[finger] + \
uppercase_keys[finger])
if(2)
string += pick(numerals[finger] + lowercase_keys[finger] + \
uppercase_keys[finger] + special_chars[finger])

last_finger = finger

if(1)
var/fingers = list(1,2,3,4)
var/last_finger = pick(fingers)

while(length--)
var/finger = pick(fingered ? fingers - last_finger : fingers)

switch(mixedcase)
if(-1)
switch(alpha)
if(-1)
string += pick(uppercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + uppercase_keys[finger])
if(2)
string += pick(numerals[finger] + uppercase_keys[finger] + \
special_chars[finger])
if(0)
switch(alpha)
if(-1)
string += pick(lowercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + lowercase_keys[finger])
if(2)
string += pick(numerals[finger] + lowercase_keys[finger] + \
special_chars[finger])
if(1)
switch(alpha)
if(-1)
string += pick(uppercase_keys[finger] + lowercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + lowercase_keys[finger] + \
uppercase_keys[finger])
if(2)
string += pick(numerals[finger] + lowercase_keys[finger] + \
uppercase_keys[finger] + special_chars[finger])

last_finger = finger

if(2)
var/fingers = list(5,6,7,8)
var/last_finger = pick(fingers)

while(length--)
var/finger = pick(fingered ? fingers - last_finger : fingers)

switch(mixedcase)
if(-1)
switch(alpha)
if(-1)
string += pick(uppercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + uppercase_keys[finger])
if(2)
string += pick(numerals[finger] + uppercase_keys[finger] + \
special_chars[finger])
if(0)
switch(alpha)
if(-1)
string += pick(lowercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + lowercase_keys[finger])
if(2)
string += pick(numerals[finger] + lowercase_keys[finger] + \
special_chars[finger])
if(1)
switch(alpha)
if(-1)
string += pick(uppercase_keys[finger] + lowercase_keys[finger])
if(0)
string += pick(numerals[finger])
if(1)
string += pick(numerals[finger] + lowercase_keys[finger] + \
uppercase_keys[finger])
if(2)
string += pick(numerals[finger] + lowercase_keys[finger] + \
uppercase_keys[finger] + special_chars[finger])

last_finger = finger
return string



///*
//Testing code/sample implementation:

mob/verb/test_password()
var/randpwd/passwordgen = new

var/mixedcase = pick(-1,0,1)
var/alpha = pick(-1,0,1,2)
var/hands = pick(-1,1,2)

switch(mixedcase)
if(-1) usr << "Upper-case, \..."
if(0) usr << "Lower-case, \..."
if(1) usr << "Mixed-case, \..."

switch(alpha)
if(-1) usr << "alphabetic, \..."
if(0) usr << "numeric, \..."
if(1) usr << "alphanumeric, \..."
if(2) usr << "random character, \..."

switch(hands)
if(-1) usr << "full keyboard \..."
if(1) usr << "left-handed \..."
if(2) usr << "right-handed \..."


usr << "password: <tt>[passwordgen.GeneratePassword(length=6, mixedcase=mixedcase,\
alpha=alpha, hands=hands)]
</tt>"
//*/