{"id":46,"date":"2012-05-07T18:10:12","date_gmt":"2012-05-07T17:10:12","guid":{"rendered":"http:\/\/www.datenzone.de\/blog\/?p=46"},"modified":"2012-05-07T18:13:52","modified_gmt":"2012-05-07T17:13:52","slug":"generating-safe-prime-numbers-in-python","status":"publish","type":"post","link":"https:\/\/www.datenzone.de\/blog\/2012\/05\/generating-safe-prime-numbers-in-python\/","title":{"rendered":"Generating safe prime numbers in Python"},"content":{"rendered":"<p>Recently, I tried to generate <a href=\"https:\/\/en.wikipedia.org\/wiki\/Safe_prime\">safe prime numbers<\/a> in a Python program. Python is a nice language when it comes to implementing code that needs to deal with long numbers and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Arbitrary-precision_arithmetic\">arbitrary precision integer\u00c2\u00a0arithmetic<\/a>. Python has build in support for arbitrary precision integers, and operators like +, *, or ** <a href=\"http:\/\/docs.python.org\/reference\/datamodel.html\">can be overloaded<\/a>.<\/p>\n<p>If the native support for long numbers in Python seems to be to slow, there is <a href=\"https:\/\/code.google.com\/p\/gmpy\/\">gmpy<\/a>, a Python C-binding, that allows you to use the <a href=\"http:\/\/gmplib.org\/\">GMP library<\/a> from your python code. Due to the nice operator overloading in Python, you don&#8217;t need to change anything in your calculations, except for the initialization of your data.<\/p>\n<p>GMP supports finding prime numbers and also efficient prime testing, bue there is no support for generating safe prime numbers in python and\/or in GMP. A number <img src='https:\/\/s0.wp.com\/latex.php?latex=p&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p' title='p' class='latex' \/> is a safe prime number, if <img src='https:\/\/s0.wp.com\/latex.php?latex=p&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p' title='p' class='latex' \/> is prime, and <img src='https:\/\/s0.wp.com\/latex.php?latex=%28p-1%29%2F2&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='(p-1)\/2' title='(p-1)\/2' class='latex' \/> is a prime number too. <a href=\"https:\/\/www.openssl.org\/\">OpenSSL<\/a> supports the generation of such numbers. So I decided to write an OpenSSL Python binding, to make it possible to generate these numbers in a Python script, without having to call an external program.<\/p>\n<p>My implementation gensafeprime can be <a href=\"https:\/\/github.com\/eriktews\/gensafeprime\">downloaded<\/a> from github, and is also <a href=\"http:\/\/pypi.python.org\/pypi\/gensafeprime\">available<\/a> on PyPi. Using the code is easy. The following example will generate a 512 bit safe prime number:<\/p>\n<pre lang=\"Python\">#!\/usr\/bin\/python\r\n\r\nimport gensafeprime\r\nprint gensafeprime.generate(512)<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I tried to generate safe prime numbers in a Python program. Python is a nice language when it comes to implementing code that needs to deal with long numbers and arbitrary precision integer\u00c2\u00a0arithmetic. Python has build in support for &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/www.datenzone.de\/blog\/2012\/05\/generating-safe-prime-numbers-in-python\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-crypto"],"_links":{"self":[{"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":8,"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.datenzone.de\/blog\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}