Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Passwords, hashes, and salt

by waswas-fng (Curate)
on Jun 24, 2005 at 18:37 UTC ( #469789=note: print w/replies, xml ) Need Help??


in reply to Passwords, hashes, and salt

There is really no need to use someone else's implementation unless you need to share the auth with another application. If you have full control of the app, a good way to do it is to use sha1 digests with a salt. such as:
use strict; use Digest::SHA1; my $plaintext="Test1ng"; #generate a two char salt... my $salts= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345 +6789./"; my $s1 = rand(64); my $s2 = rand(64); my $salt = substr($salts,$s1,1) . substr($salts,$s2,1); my $sha1 = Digest::SHA1->new; $sha1->add($salt.$plaintext); # the enc pass is presented with the plaintext salt as the first two c +hars. my $encpass = $salt. $sha1->hexdigest; print "$encpass\n"; This outputs: kcc68a68507e8636a1a9a6badc059342b19a12c58e And to test the password: use strict; use Digest::SHA1; my $storedpass='kcc68a68507e8636a1a9a6badc059342b19a12c58e'; my $plaintext="Test1ng"; #pull salt from the saved password so you can compair... my $salt = substr($storedpass,0,2) ; my $sha1 = Digest::SHA1->new; $sha1->add($salt.$plaintext); # the enc pass is presented with the plain-text salt as the first two +chars. my $encpass = $salt. $sha1->hexdigest; print "if $encpass = $storedpass the password is correct...\n"; which outputs: perl testpass.pl if kcc68a68507e8636a1a9a6badc059342b19a12c58e = kcc68a68507e8636a1a9a6 +badc059342b19a12c58e the password is correct...
The reason you "salt" the plain-text is so that if you have 30,000 users in the password database and you want to check to see if any use the password "greatwork" you have to digest it against up to 30,000 salts instead of just just digesting it once and moving on to the next word in the attack. Adding more than 2 random chars to a salt potentially adds more difficulty as you have more users in the database, as there would be less salt overlap. I hope this makes sense...


-Waswas

Replies are listed 'Best First'.
Re^2: Passwords, hashes, and salt
by Mr_Person (Hermit) on Jun 24, 2005 at 19:15 UTC
    Okay, that looks pretty good. The main reason I wanted a premade module is that I've seen a lot of examples where people tried to impliment their own encryption/security schemes instead of going with something that's been proven and got burned by it. When it comes to something like cryptography that I don't fully understand, I'd prefer to use code written by someone that has a better understanding than myself.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://469789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2019-05-22 04:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (138 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!