Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/cybertivity/powerjournal/PasswordLib.java


1   package com.cybertivity.powerjournal;
2   import java.security.*;
3   import java.text.*;
4   import java.util.*;
5   
6   /**
7    * Title:        PowerJournal
8    * Description:  $Id: PasswordLib.java,v 1.1 2001/12/01 21:02:13 arrowood Exp $
9    * Copyright:    Copyright (c) 2001
10   * Company:      <A HREF="http://www.cybertivity.com">Cybertivity</A>
11   *
12   * @author <A HREF="mailto:chris.arrowood@cybertivity.com">Chris Arrowood</A>
13   * @created November 30, 2001
14   * @version 1.0
15   */
16  
17  public class PasswordLib {
18  
19    public static String hashPassword(String tryPassword)
20         throws java.security.NoSuchAlgorithmException {
21  
22      if (tryPassword == null) {
23        throw new NoSuchAlgorithmException("Null Password");
24      }
25      try {
26        String hashedPassword = "";
27        MessageDigest algorithm = null;
28        algorithm = MessageDigest.getInstance("SHA-1");
29        algorithm.reset();
30        byte[] tempbuffer = tryPassword.getBytes();
31        // create byte[] array
32        algorithm.update(tempbuffer);
33        //hash it
34        byte[] digestedBytes = algorithm.digest();
35  
36        //Now Base64 encode this so it will go nicely in the database
37        byte[] base64Buffer = (Base64.encode(digestedBytes)).getBytes();
38  
39        for (int i = 0; i < base64Buffer.length; i++) {
40          //create String in loop
41          hashedPassword = hashedPassword + (char) base64Buffer[i];
42        }
43  
44        return hashedPassword;
45        //return
46      } catch (NoSuchAlgorithmException ex) {
47        throw new NoSuchAlgorithmException("NoSuchAlgorithmException" + ex);
48      }
49  
50    }
51  }