php - Is substr safe for password reset? -


i wondering if using substr(md5(rand()), 0, 17); safe enough password reset link? if generate longer string make safer? md5 @ safe? or should $token = sha1(uniqid($username, true));?

the use of substr() or md5() secondary use of rand().

the whole point of using password reset tokens they're unpredictable , rand() known weak due underlying lcg model.

it better idea use system's entropy source instead, e.g.:

$rand = openssl_random_pseudo_bytes(8); // take 8 random bytes $token = substr(md5($rand), 0, 17); 

it takes bytes system's random source, e.g. /dev/urandom on linux or corresponding system windows.

note if don't have particular size constraint might choose full sha1() output , take 16 random bytes.

also, should treat password reset tokens if (temporary, time limited) passwords when store them in database; suggest send above token user , use password_hash() before write them in database. @ later stage check given token (assuming it's not expired) using password_verify().


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -