webForumDet fria alternativet

Tips på hashning?

6 svar · 830 visningar · startad av bassebhu

bassebhuMedlem sedan nov. 20016 480 inlägg
#1

Hej alla glada!

Har sökt i forumet, men hittade inga konkreta helt aktuella kodsnuttar :)

Har läst att både md5 och sha1 är "ute" och att man nu bör använda hash().
Stämmer detta?

Vad jag förstår så är det bäst att spara 1) användarnamn, 2) ett slumpat salt samt 3) användarnamn plus salt hashat i databasen? Någon som sitter på ett kodförslag för detta?

Och det här med SSL som alla tipsar om, hur funkar det egentligen? :)

Tack!

JaktJanneMedlem sedan feb. 20001 507 inlägg
#2

Använda hash()?
Enligt php.net (http://se2.php.net/manual/en/function.hash.php) så består funktionen av följande:
hash(algoritm, text_som_skall_hashas)
Vilket jag tolkar som att man kan använda något av de algoritmer som finns, på sidan som jag länkar till finns det i exemplen lite jämförelse.

bassebhuMedlem sedan nov. 20016 480 inlägg
#3

Det första inlägget i den artikeln innehöll:

The well known hash functions MD5 and SHA1 should be avoided in new applications. Collission attacks against MD5 are well documented in the cryptographics literature and have already been demonstrated in practice. Therefore, MD5 is no longer secure for certain applications.

Collission attacks against SHA1 have also been published, though they still require computing power, which is somewhat out of scope. As computing power increases with time and the attacks are likely to get better, too, attacks against systems relying on SHA1 for security are likely to become feasible within the next few years.

There is no lack of potential alternative hash algorithms, as the many choices for the "algo" argument of PHPs hash() function already suggests. Unfortunately, there is lack of analysis, as to how secure these alternative algorithms are. It is rather safe to assume, though, that the SHA2 family with its most prominent members SHA-256 und SHA-512, is better than SHA1.

When storing password hashes, it is a good idea to prefix a salt to the password before hashing, to avoid the same passwords to hash to the same values and to avoid the use of rainbow tables for password recovery. Unlike suggested in other articles, there is no security advantage in putting the salt in the middle, or even at both the beginning and the end, of the combined salt-password-string.

Rather, there are two other factors, that determine the strength of the salt: Its length and its variability. For example, using the same salt for all passwords is easy to implement, but gives only very little additional security. In particular, if users type the same passwords, they will still hash to the same value!

Therefore, the salt should be random string with at least as many variable bits, as there are bits in the hash result. In the user database, store username, the randomly generated salt for that user, and the result of hashing the salt-password-string. Access authentication is then done by looking up the entry for the user, calculating the hash of the salt found in the database and the password provided by the user, and comparing the result with the one stored in the database.

Detta gör ju att man blir lite fundersam över vad man ska välja :)

Tacksam för vägledning!

chillOutMedlem sedan nov. 20095 inlägg
#4

sha1 med salt brukar jag använda, ska vara fullt tillräckligt!

ercizMedlem sedan maj 20011 826 inlägg
#5

Men samtidigt. Vad är det du vill skydda egentligen?
Hashningen knäcks ganska enkelt idag, oavsett vilken algorithm. Det är bara en fråga om hur lång tid det tar innan det är knäckt. Det vikitga är att få användarna att välja starka lösenord.

Men för att kunna knäcka hasherna så krävs åtkomst till din databas. Så det är viktigare att den är ordentligt skyddad.

Ang. SSL så är det ju ett helt annat område. Med SSL så får du en krypterad anslutning mellan besökarens webbsida och servern. Utan SSL som de flesta sajter tråkigt nog använder för det mesta så skickas all trafrik, inklusive lösenord, okrypterat mellan webbläsaren och servern. Det gör att någon emellan kan läsa allt. T.ex. nån på ett café om besökaren sitter på ett café med wlan.

spangoMedlem sedan juni 20008 205 inlägg
#6

bassebhu skrev:

Det första inlägget i den artikeln innehöll:

[...]
There is no lack of potential alternative hash algorithms, as the many choices for the "algo" argument of PHPs hash() function already suggests. Unfortunately, there is lack of analysis, as to how secure these alternative algorithms are. It is rather safe to assume, though, that the SHA2 family with its most prominent members SHA-256 und SHA-512, is better than SHA1.
[...]

Detta gör ju att man blir lite fundersam över vad man ska välja :)

Som det står i texten du citerar; SHA-2 familjen är antagligen hyggligt säker en tid framöver (men det finns en anledning till varför man i skrivande stund jobbar på SHA-3). För att använda t.ex. SHA-256 anropar du hash('sha256', $password . $salt). Mer avancerade kodexempel torde du kunna hitta om du söker på "hash salt" här på wF, frågan har avhandlats ett oändligt antal gånger, men principen är:

  1. Lösenordet + saltet sparas hashat
  2. Saltet sparas i klartext
  3. Gör hashningen flera gånger och spara hur många iterationer du använt för att kunna tweaka i efterhand (100 hashningar gör att det tar 100 gånger längre att brute force-knäcka lösenord - "big O"-komplexiteten förändras inte, men den faktiska tiden det tar att knäcka lösenordet gör det).
  4. Spara gärna vilken hashalgoritm som använts tillsammans med iterationsantalet, saltet och hashen, så att du kan byta upp dig när det kommer nya.
  5. Bry dig inte om att hasha lösenordet utan salt (då kan det lika gärna vara ohashat), eller att lägga med användarnamn eller något annat icke-slumpmässigt i hashen, det gör i bästa fall ingen skillnad och i värsta fall kan det försämra säkerheten.

Pseudokod:

class Hashed_password
    const int ITERATIONS = 100, SALTLEN = 32
    const string ALGO = 'sha256'

    string salt, hashed, algo
    int iterations, salt_len

    constructor(string password)
        this.salt_len = SALTLEN
        this.salt = randomStringOfLength(this.salt_len)
        this.algo = ALGO
        this.iterations = ITERATIONS
        this.hashed = make_hash(password)
    
    function make_hash(string password) : string
        string result = concat(password, this.salt)
        for (int i = 0 .. this.iterations)
            result = hash(this.algo, result)
        return result

    function is_correct_password(string password) : boolean
        return this.hashed == this.make_hash(password)

...

var p = new Hashed_password('Roflc0pt£r')
if p.is_correct_password('Roflc0pt£r')
    // ok lösenord
else
    // felaktigt lösenord
spangoMedlem sedan juni 20008 205 inlägg
#7

... men jag skulle nog rekommendera att man använder nåt nån annan redan gjort, typ bcrypt, eftersom oddsen att man förr eller senare screwar upp är ganska stora. Finns stöd för bcrypt i phpass: http://www.openwall.com/phpass/

138 ms totalt · 3 externa anrop · v20260731065814-full.0e50b1ef
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
135 ms — hämta tråd, inlägg och bilagor (db)