Tiens il me semblait bien que j'avais déjà écrit quelque chose comme ça. Cela nécessite bien sûr une clé Google API que tu peux obtenir très simplement ici [
http://code.google.com/apis/ajaxsearch/signup.html ] en entrant
http://localhost tout simplement)
Je te mets tout ça brut de béton c'est un peu commenté tu devrais t'en sortir si tu connais un peu JavaScript. Note que les codes commencent par "<?php" il faut bien sûr le virer ce n'est que pour la coloration syntaxique.
test-password.html (démo d'utilisation)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>My Google AJAX Search API Application</title>
<link href="http://www.google.com/uds/css/gsearch.css" type="text/css" rel="stylesheet"/>
<script src="http://www.google.com/uds/api?file=uds.js&v=1.0&key=MA-CLE-GOOGLE-API-ICI" type="text/javascript"></script>
<script language="Javascript" type="text/javascript" src="passwordcheck.js"></script>
<script type="text/javascript" type="text/javascript" src="test-password.js"></script>
</head>
<body>
<input type="text" id="password" onchange="pc.evaluate(this.value)" />
<div id="wait" style="display:none">Calcul...</div>
<div id="affichage-score"></div>
<div id="search"></div>
</body>
</html>
passwordcheck.js (la librairie en elle-même)
// from prototype.js
if (!Object.extend) {
Object.extend = function(o1, o2) {
for (key in o2) {
o1[key] = o2[key];
}
return o1;
}
}
function PasswordChecker(options) {
// public readonly : uniqid attributed for public access (needed for setTimeout calls)
this.idprefix = '__pswd__chk__';
this.uniqid = 0;
// private : google API components (only if it exists)
if (GSearchControl && GwebSearch) {
this.sc = new GSearchControl();
this.ws = new GwebSearch();
}
else {
this.sc = false;
this.ws = false;
}
// options
this.options = Object.extend({
'scoreWordExists' : -5, // score added when password found in google
'scoreLowCase' : +1, // score added when lower case characters [a-z] used
'scoreUpCase' : +1, // score added when upper case characters [A-Z] used
'scoreDigit' : +1, // score added when digits [0-9] used
'scoreNonAlnum' : +1, // score added when other charaters [^a-zA-Z0-9] used
'scoreSlice' : +1, // score added per each slice found
'sliceSize' : 6, // size of each slice
'scoreMin' : 0, // min score (lower scores are brought to this score)
'scoreMax' : 8, // max score (bigger scores are brought to this score)
'callback' : function(score) { alert(score) }, // function called to display the score, you must defined this one
'onStart' : function() {}, // function called just before calculus
'onEnd' : function() {}, // function called after calculus, just before callback
'googleSrcLayer' : document.createElement('div') // layer where google search results are displayed (default = mute layer)
}, options);
// initialize object only when "evaluate is called"
this.initialized = false;
this.initialize = function() {
if (this.sc) {
// background google search control
this.sc.setResultSetSize(GSearch.LARGE_RESULTSET);
this.sc.addSearcher(this.ws);
this.sc.draw(this.options.googleSrcLayer);
this.sc.setTimeoutInterval(GSearchControl.TIMEOUT_MEDIUM); // medium : about 500ms
}
// unique id for globalization
while (typeof window[this.idprefix+this.uniqid] != 'undefined') {
this.uniqid++;
}
window[this.idprefix+this.uniqid] = this;
this.initialized = true;
}
// private : used to follow the password in public calls (needed for setTimeout)
this.checkedPassword = '';
// public : evaluate score of parameter
this.evaluate = function(password) {
if (!this.initialized) {
this.initialize();
}
this.options.onStart(); // 'onStart' is called here
if (!this.options.scoreWordExists || !this.sc) {
// direct evaluation, no need to search on google (or can't because API unavailable)
this.score = this.directEvaluate(password);
this.options.onEnd(); // 'onEnd' called before callback
this.options.callback(Math.min(this.options.scoreMax,Math.max(this.options.scoreMin,score)));
}
else {
// search on google, and then give result + basic evaluation
this.ws.clearResults();
this.sc.execute(password);
this.checkedPassword = password;
this.checkSearchState(0);
}
}
// public but must not be called directly : called regularly to check state of the google search
this.checkSearchState = function(elapsedTime) {
if (elapsedTime > GSearchControl.TIMEOUT_MEDIUM || this.ws.results.length > 0) {
var score = this.directEvaluate(this.checkedPassword);
if (this.ws.results.length > 0) {
score += this.options.scoreWordExists
}
this.options.onEnd(); // 'onEnd' called before callback
this.options.callback(Math.min(this.options.scoreMax,Math.max(this.options.scoreMin,score)));
}
else {
setTimeout('window.'+this.idprefix+this.uniqid+'.checkSearchState('+(elapsedTime+100)+')', 100);
}
}
// public : returns score of the password without google search (only characters-analysis)
this.directEvaluate = function(password) {
var useUpCase = 0;
var useLowCase = 0;
var useNonAlnum = 0;
var useDigit = 0;
var slices = Math.floor(password.length/this.options.sliceSize);
for (var i=0; i<password.length; i++) {
var code = password.charCodeAt(i);
if (97 <= code && code <= 122) {
useLowCase = 1;
}
else if (65 <= code && code <= 90) {
useUpCase = 1;
}
else if (48 <= code && code <= 57) {
useDigit = 1;
}
else {
useNonAlnum = 1;
}
}
return useUpCase * this.options.scoreUpCase
+ useLowCase * this.options.scoreLowCase
+ useDigit * this.options.scoreDigit
+ useNonAlnum * this.options.scoreNonAlnum
+ slices * this.options.scoreSlice;
}
}
test-password.js (le script de la page de démo, exemple d'initialisation de l'objet)
var pc = new PasswordChecker({
'scoreMin' : 0,
'scoreMax' : 6,
'onStart' : function() {
document.getElementById('wait').style.display = 'block';
},
'onEnd' : function() {
document.getElementById('wait').style.display = 'none';
},
'callback' : function(score) {
var layer = document.getElementById('affichage-score');
layer.innerHTML = score+'/6';
}
});