{"id":1402,"date":"2012-08-23T06:00:56","date_gmt":"2012-08-23T04:00:56","guid":{"rendered":"http:\/\/www.manuelmarangoni.it\/sir-bit\/?p=1402"},"modified":"2016-02-11T12:24:25","modified_gmt":"2016-02-11T11:24:25","slug":"javascript-generare-automaticamente-una-password-casuale","status":"publish","type":"post","link":"https:\/\/www.manuelmarangoni.it\/sir-bit\/1402\/javascript-generare-automaticamente-una-password-casuale\/","title":{"rendered":"Javascript &#8211; Generare automaticamente una password casuale"},"content":{"rendered":"<p>Generare una password casuale con javascript \u00e8 molto semplice.<\/p>\n<p>Dal lato del codice, \u00e8 sufficiente importare un javascript di poche righe che si occupa di personalizzare la tipologia di password voluta e la sua lunghezza.<br \/>\nIl codice che trovate qui sotto, liberamente scaricabile, \u00e8 completo e funzionante e permette di <strong>generare una password alfanumerica con maiuscole e minuscole, di una lunghezza casuale<\/strong> compresa tra 6 e 15.<\/p>\n\n        <h2>Download script<\/h2>\n        <div style=\"float:left; width:100%; margin-top:60px; margin-bottom:40px;\">\n        <div style=\"float:left; width:100px;\"><img decoding=\"async\" title=\"[JAVASCRIPT] Generare password casuale\" src=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/themes\/sirbit\/images\/redirect_icon.png\" alt=\"[JAVASCRIPT] Generare password casuale\" \/><\/div>\n        <div style=\"float:left; width:350px; padding-top:30px; font-size:14px;\"><strong><a title=\"[JAVASCRIPT] Generare password casuale\" href=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/code\/javascript-generare-password-casuale\/\">Pagina diretta alla scheda e al download del codice<\/a><\/strong><\/div>\n        <\/div>\n        <div style=\"clear:both;\"> <\/div>\n<p>Vediamo in breve come personalizzarlo.<\/p>\n<h2>Il form<\/h2>\n<p>Nel mio caso ho deciso di <strong>immagazzinare la stringa-password in un campo input<\/strong>. Alla pressione sul link, si richiamer\u00e0 lo script javascript che generer\u00e0 la password e la metter\u00e0 in automatico come valore al campo input.<br \/>\nNulla vieta, per\u00f2, che possiamo semplicemente immagazzinarla in una variabile per inviarla via posta o stamparla a video.<br \/>\n<!--more--><\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"html4strict\" style=\"font-family: monospace;\">\u00a0\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">script<\/span> <span style=\"color: #000066;\">type<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"text\/javascript\"<\/span> <span style=\"color: #000066;\">src<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"javascript.js\"<\/span>&gt;&lt;<span style=\"color: #66cc66;\">\/<\/span><span style=\"color: #000000; font-weight: bold;\">script<\/span>&gt;<\/span> <span style=\"color: #808080; font-style: italic;\">&lt;!-- importazione delle funzioni javascript --&gt;<\/span>\r\n\r\n[...]\r\n\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">body<\/span>&gt;<\/span>\r\n\r\n\t<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">input<\/span> <span style=\"color: #000066;\">type<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"text\"<\/span> <span style=\"color: #000066;\">name<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"password\"<\/span> <span style=\"color: #000066;\">id<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"password\"<\/span> <span style=\"color: #000066;\">value<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"\"<\/span>&gt;<\/span>\r\n\t<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n\r\n\t<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">a<\/span> <span style=\"color: #000066;\">href<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"javascript:void(0)\"<\/span> <span style=\"color: #000066;\">onClick<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"crea_password();\"<\/span>&gt;<\/span>Clicca qui per creare una password casuale<span style=\"color: #009900;\">&lt;<span style=\"color: #66cc66;\">\/<\/span><span style=\"color: #000000; font-weight: bold;\">a<\/span>&gt;<\/span>\r\n\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #66cc66;\">\/<\/span><span style=\"color: #000000; font-weight: bold;\">body<\/span>&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>Lo script javascript<\/h2>\n<p>Le <strong>variabili personalizzabili sono tre<\/strong>:<br \/>\n<code>elencoCaratteri<\/code> : la password sar\u00e0 generata tenendo conto dei soli caratteri presenti in questa variabile. Quindi eventuali simboli vanno aggiunti all&#8217;interno di questa stringa.<br \/>\n<code>minimoCaratteri<\/code> e <code>massimoCaratteri<\/code> : la lunghezza della stringa sar\u00e0 racchiusa tra questi valori.<\/p>\n<p>L&#8217;ultima riga (riga 19) si occupa di associare la password generata al valore dell&#8217;elemento che ha come id &#8220;password&#8221; (nel mio caso, il campo input).<\/p>\n\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"javascript\" style=\"font-family: monospace;\"><span style=\"color: #003366; font-weight: bold;\">function<\/span> crea_password<span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\r\n\t<span style=\"color: #003366; font-weight: bold;\">var<\/span> elencoCaratteri<span style=\"color: #339933;\">=<\/span><span style=\"color: #3366cc;\">\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\"<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t<span style=\"color: #003366; font-weight: bold;\">var<\/span> minimoCaratteri<span style=\"color: #339933;\">=<\/span><span style=\"color: #cc0000;\">6<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t<span style=\"color: #003366; font-weight: bold;\">var<\/span> massimoCaratteri<span style=\"color: #339933;\">=<\/span><span style=\"color: #cc0000;\">15<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t<span style=\"color: #003366; font-weight: bold;\">var<\/span> differenzaCaratteri<span style=\"color: #339933;\">=<\/span>massimoCaratteri<span style=\"color: #339933;\">-<\/span>minimoCaratteri<span style=\"color: #339933;\">;<\/span>\r\n\r\n\t<span style=\"color: #003366; font-weight: bold;\">var<\/span> lunghezza<span style=\"color: #339933;\">=<\/span>Math.<span style=\"color: #660066;\">round<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">(<\/span>Math.<span style=\"color: #660066;\">random<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">*<\/span>differenzaCaratteri<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">+<\/span>minimoCaratteri<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t<span style=\"color: #003366; font-weight: bold;\">var<\/span> incremento<span style=\"color: #339933;\">=<\/span><span style=\"color: #cc0000;\">0<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t<span style=\"color: #003366; font-weight: bold;\">var<\/span> password<span style=\"color: #339933;\">=<\/span><span style=\"color: #3366cc;\">\"\"<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t<span style=\"color: #000066; font-weight: bold;\">while<\/span><span style=\"color: #009900;\">(<\/span>incremento<span style=\"color: #339933;\">&lt;<\/span>lunghezza<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\t   password<span style=\"color: #339933;\">+=<\/span>elencoCaratteri.<span style=\"color: #660066;\">charAt<\/span><span style=\"color: #009900;\">(<\/span>Math.<span style=\"color: #660066;\">round<\/span><span style=\"color: #009900;\">(<\/span>Math.<span style=\"color: #660066;\">random<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">*<\/span>elencoCaratteri.<span style=\"color: #660066;\">length<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t   incremento<span style=\"color: #339933;\">++;<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\tdocument.<span style=\"color: #660066;\">getElementById<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #3366cc;\">'password'<\/span><span style=\"color: #009900;\">)<\/span>.<span style=\"color: #660066;\">value<\/span><span style=\"color: #339933;\">=<\/span>password<span style=\"color: #339933;\">;<\/span>\r\n\r\n<span style=\"color: #009900;\">}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Uno script liberamente scaricabile per generare una password casuale con javascript, alla pressione su un link. Lunghezza e caratteri sono personalizzabili.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[12,6],"class_list":["post-1402","post","type-post","status-publish","format-standard","hentry","category-javascript-e-derivati","tag-javascript","tag-script","cat-80-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1402"}],"collection":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/comments?post=1402"}],"version-history":[{"count":7,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1402\/revisions"}],"predecessor-version":[{"id":2370,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1402\/revisions\/2370"}],"wp:attachment":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/media?parent=1402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/categories?post=1402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/tags?post=1402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}