Javascript:
Code:
function PasswordGenerator(defaultLength) {
var i;
this.length = defaultLength || 8;
this.data = [];
this.weighted = [];
for(i = 0x20; i < 0x7F; ++i)
this.data.push(
{
'weight' : 1,
'datum' : String.fromCharCode(i)
}
);
this.recalcWeights();
}
PasswordGenerator.prototype.setLength = function(length) {
var l;
if(!isNaN(l = parseInt(length)))
return (this.length = length);
else return this.length;
};
PasswordGenerator.prototype.clearData = function() {
this.data = [];
this.recalcWeights();
};
PasswordGenerator.prototype.findDatum = function(datum) {
for(var i = 0; i < this.data.length; ++i)
if(this.data[i].datum === datum)
return i;
return -1;
};
PasswordGenerator.prototype.addData = function(data, weight) {
var weight = parseInt(weight, 10);
if(isNaN(weight))
weight = 1;
for(var i = 0, c; !isNaN((c = data.charAt(i)).charCodeAt(0)); ++i)
this.addDatum(c, weight);
};
PasswordGenerator.prototype.addDatum = function(datum, weight) {
var weight = parseInt(weight, 10);
if(isNaN(weight))
weight = 1;
if(this.findDatum(datum) !== -1)
this.removeDatum(datum);
this.data.push({'datum' : datum, 'weight' : weight});
this.recalcWeights();
};
PasswordGenerator.prototype.removeDatum = function(datum) {
var index = this.findDatum(datum);
if(index === -1)
return null;
this.data.splice(index, 1);
this.recalcWeights();
};
PasswordGenerator.prototype.recalcWeights = function() {
for(var i = 0; i < this.data.length; ++i)
for(var j = 0; j < this.data[i].weight; ++j)
this.weighted.push(this.data[i].datum);
};
PasswordGenerator.prototype.getCharacter = function() {
return this.weighted[Math.floor(Math.random() * this.weighted.length)];
};
PasswordGenerator.prototype.generate = function() {
var ret = "";
for(var i = 0; i < this.length; ++i)
ret += this.getCharacter();
return ret;
};
Or PHP:
Code:
class PasswordGenerator {
var $length;
var $data;
var $weighted;
function PasswordGenerator($defaultLength = 8) {
$this->length = $defaultLength;
$this->data = array();
$this->weighted = array();
for($i = 0x20; $i < 0x7F; ++$i)
array_push($this->data, array('weight' => 1, 'datum' => chr($i)));
$this->recalcWeights();
}
function setLength($length = 0) {
if($length < 1) return $this->length;
return $this->length;
}
function clearData() {
$this->data = array();
$this->recalcWeights();
}
function findDatum($datum) {
foreach($this->data as $key => $val)
if($val === $datum)
return $key;
return -1;
}
function addData($data, $weight = 1) {
$c;
for($i = 0; (ord($c = $data[$i])); ++$i)
$this->addDatum($c, $weight);
}
function addDatum($datum, $weight = 1) {
if($this->findDatum($datum) !== -1)
$this->removeDatum($datum);
array_push($this->data, array('datum' => $datum, 'weight' => $weight));
$this->recalcWeights();
}
function removeDatum($datum) {
$index = $this->findDatum($datum);
if($index === -1)
return null;
array_splice($this->data, $index, 1);
$this->recalcWeights();
}
function recalcWeights() {
foreach($this->data as $key => $val)
for($j = 0; $j < $val['weight']; ++$j)
array_push($this->weighted, $val['datum']);
}
function getCharacter() {
return $this->weighted[rand(0, count($this->weighted) - 1)];
}
function generate() {
$ret = "";
for($i = 0; $i < $this->length; ++$i)
$ret .= $this->getCharacter();
return $ret;
}
}
Tested slightly but not extensively.
Bookmarks