Skip to main content

Password Hash

Posted by evanx on February 21, 2007 at 11:40 PM PST

This series is about defining, styling and/or generating documents and reports,
using Java code, eg. as an alternative to XML or even HTML/CSS templates,
even though the primary target is typically HTML/CSS. But later we wanna
output PDF also (eg. for business documents and reports, eg. using iText),
maybe LaTeX for articles (altho i think HTML is fine, and printing from
Firefox and using PDFCreator is quite OK), and Excel is really great for reports
(and we can use Apache POI), and of course we wanna support ODF cos it's nice,
and OpenOffice it's nice. For starters we'll look at how articles can be styled
and processed using quitehyper. This first article looks at style objects (for CSS).
The next article will look at generating CSS using those styles.
Later we'll look at processing HTML eg. to apply those styles using CSS,
but most importantly, to perform syntax highlighting of preformatted code snippet blocks.
-->

Passwords should never be seen in clear text eg. in transfer objects,
or in database columns. So we hash them up. Nothing to it.

href="http://aptframework.dev.java.net/jelly/passwordHash.html">

border="0" width="32" height="32" align="left" hspace="8"/>
Click here to read "Password Hash, a short story"

Part of the "Jelly Beans" part of a trilogy in 42 parts

style="text-decoration: none;">



Code Snippet

public class PasswordHasher {
    String algorithm = "SHA-256";
   
    public String hashPassword(byte[] passwordBytes)
    throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        byte[] hashBytes = digest.digest(passwordBytes);
        String hashString = Base64.encode(hashBytes);
        return hashString;
    }
   
    public boolean verifyPassword(byte[] passwordBytes, String hashString)
    throws NoSuchAlgorithmException {
        return hashPassword(passwordBytes).equals(hashString);
    }
}   


style="text-decoration: none;">
style="text-decoration: none;">
style="text-decoration: none;">