{"id":410,"date":"2012-05-17T09:35:41","date_gmt":"2012-05-17T09:35:41","guid":{"rendered":"http:\/\/www.plugged.in\/?p=410"},"modified":"2012-05-17T09:35:41","modified_gmt":"2012-05-17T09:35:41","slug":"password-generation-in-unix","status":"publish","type":"post","link":"https:\/\/www.veriteknik.net.tr\/en\/password-generation-in-unix\/","title":{"rendered":"Password Generation in UNIX"},"content":{"rendered":"<p>Below I&#8217;ll describe a couple of nice methods to generate passwords using Python and Bash.<\/p>\n<p>Actually there are a lot of ways you can accomplish this especially with bash, but using the <strong>\/dev\/urandom<\/strong> file seems to be the most clever one.<\/p>\n<p>The <strong>\/dev\/urandom<\/strong> device doesn&#8217;t only generate read-friendly characters, so it&#8217;s best to filter out the ones we&#8217;d like. The best tool for that would be <strong>tr<\/strong>.<\/p>\n<p><strong>$ cat \/dev\/urandom | tr -dc [:alnum:] | head -c 10<\/strong><\/p>\n<p>This will generate a password from 10 alphanumeric characters.<\/p>\n<p>It will not include some characters though, such as <strong>. ! &#8211; _<\/strong> which are useful for passwords. So this line would be a little more &#8220;secure&#8221;.<\/p>\n<p><strong>$ cat \/dev\/urandom | tr -cd &#8220;[:alnum:]\\.\\-_\\!&#8221; | head -c 10<br \/>\n<\/strong><br \/>\nTo generate a password in Python, using the <strong>string<\/strong> and <strong>random<\/strong> module would be a clever touch. Let&#8217;s try something like this,<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">&gt;&gt;&gt; import string, random\n &gt;&gt;&gt; def passgen(length) :\n ... keys = list(string.ascii_letters + string.digits)\n ... return \"\".join(random.choice(keys) for i in range(length)<\/pre>\n<p>With this definition of the passgen function, we can generate alphanumeric passwords with whatever length we want. If you&#8217;d like to include all characters available, try the one below:<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">&gt;&gt;&gt; import string, random\n &gt;&gt;&gt; def passgen(length) :\n ... keys = list(string.ascii_letters + string.digits + \".,;:-_()@\\\"\\\\[]?!'^+*$%&amp;\/=~`&lt;&gt;|\")\n ... return \"\".join(random.choice(keys) for i in range(length)<\/pre>\n<p>A sample output :<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">&gt;&gt;&gt; passgen(16)\n 'pP!3p\"(-uxdIqpAK'<\/pre>\n<p>You can find some methods of password generation using MD5 algorithms. For example for password generation in MySQL some people prefer this method;<\/p>\n<p><strong>&gt;SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 5)<\/strong><\/p>\n<p>But this will generate very very weak passwords, no uppercase characters and a lot of characters missing, not even to mention the non-alpha numeric characters. Also you&#8217;ll have a limit for maximum character number since the MD5 algorithm has a limit for it. So it&#8217;s best to stay away from the md5 approach for password generation. Some people also use it for bash password generation too (which is wrong! due to same reasons)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below I&#8217;ll describe a couple of nice methods to generate passwords using Python and Bash. Actually there are a lot of ways you can accomplish this especially with bash, but using the \/dev\/urandom file seems to be the most clever one. The \/dev\/urandom device doesn&#8217;t only generate read-friendly characters, so it&#8217;s best to filter out [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[372,448,399],"tags":[503],"yst_prominent_words":[1164,1166,1167,1160,1171,1158,1161,1170,1162,1157,1165,1159,1155,1169,912,619,1156,1163,1131,1168],"class_list":["post-410","post","type-post","status-publish","format-standard","hentry","category-linux_help","category-programm-in","category-security","tag-password-bash-python-md5"],"jetpack_featured_media_url":"","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Mustafa Emre Ayd\u0131n","author_link":"https:\/\/www.veriteknik.net.tr\/en\/author\/eaydin\/"},"uagb_comment_info":0,"uagb_excerpt":"Below I&#8217;ll describe a couple of nice methods to generate passwords using Python and Bash. Actually there are a lot of ways you can accomplish this especially with bash, but using the \/dev\/urandom file seems to be the most clever one. The \/dev\/urandom device doesn&#8217;t only generate read-friendly characters, so it&#8217;s best to filter out&hellip;","_links":{"self":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/comments?post=410"}],"version-history":[{"count":0,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/tags?post=410"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/yst_prominent_words?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}