java - Why the date from DB will never same format as new Date()? -
basically objective use date salt key encrypt password. using sha-512 doing this. when encrypt password, capture today's date, vardate, java.util.date type, , encryption. if output doing vardate.tostring(), see thu jul 18 17:37:27 sgt 2013. after vardate store db.
when come decryption, supply password , query vardate db, in java.util.date type, encrypt , comparison. interestingly failed because encrypted value never match. found out when output vardate, db, having format 2013-07-18 17:37:27.0.
i surprise , curious , struggling why happen? wonder whether root cause causing encryption failed?
update on 23-07-2013
this illustrate how code work on hashing algorithm. have function accept password string, , salt key string, hashing on password this:
messagedigest md = messagedigest.getinstance("sha-512"); md.reset(); md.update(saltkey.getbytes()); md.digest(password.getbytes("utf-8")); my experiment shows passing in date object, couldn't consistent result. if reformatting string, i'll consistent result. seem me date object isn't salt key candidate.
you should not use date.tostring(). output depends on actual concrete type of date, java.util.date (your first case, presumably), java.sql.timestamp (your second case, presumably) or java.sql.date. depends on current timezone.
my advice forget using dates salt completely. use securerandom generate random salt, , store salt in database.
if persist in using date, transform string using reliable mechanism, simpledateformat fixed pattern , utc timezone, example.
Comments
Post a Comment