jQuery: suggerire una password robusta ma facile da ricordare (memorable)

jQuery: suggerire una password robusta ma facile da ricordare (memorable)

In jQuery è possibile suggerire una password robusta ma facile da ricordare.

Useremo il file words.json che ha la seguente struttura:


{
  "words": [ "a", "abaco", ...]
}

Il codice HTML è molto semplice:


<p><button type="button" id="suggest">Suggerisci</button></p>
<p><code id="output"></code></p>

Ecco infine il codice jQuery:


(function( $ ) {

    $.Suggest = function() {
        this.storage = sessionStorage;
        this.num = 9999;
        this.chars = [ "?", "!", "_", "@" ];
        this.$btn = $( "#suggest" );
        this.$output = $( "#output" );

        this.init();
    };

    $.Suggest.prototype = {
        init: function() {
          var self = this;
          self.load();
          self.$btn.click(function() {
            self.$output.text( self.create() );
            return false;
          });
        },
        load: function() { // Carichiamo una sola volta i dati
          var self = this;
          $.when( $.getJSON( "words.json" ) ).
          done(function( response ) {
              // Salviamo i dati nel web storage
              self.storage.setItem( "words", JSON.stringify( response ) );
          });
        },
        create: function() {
          var self = this;
          var num = self._rand( 1, self.num );
          var chr = self._rand( 0, self.chars.length - 1 );
          var char = self.chars[chr];
          if( self.storage.getItem( "words" ) !== null ) {
            var words = JSON.parse( self.storage.getItem( "words" ) );
            var data = words.words;
            var w = self._rand( 0, data.length - 1 );
            var word = data[w];

            return word + char + num.toString();
          } else {
            return char + num.toString();
          }
        },
        _rand: function( min, max ) {
          min = Math.ceil( min );
          max = Math.floor( max );
          return Math.floor( Math.random() * ( max - min + 1 ) ) + min;
        }
    };

    $(function() {
      var sugg = new $.Suggest();
    });

})( jQuery );

La password sarà composta da una parola, un carattere speciale e un numero estratti in modo casuale. Esempio:


puro_2157

Torna su