Android SHA1 Hash Create HashPassword(String text)

Here you can find the source of HashPassword(String text)

Description

Calculates the hash value of the given password

Parameter

Parameter Description
password a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static String HashPassword(String text)
        throws NoSuchAlgorithmException, UnsupportedEncodingException 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**/*from   w w w  .  ja  v a  2  s . c o m*/
     * Calculates the hash value of the given password 
     * 
     * @param password
     * @return
     * @throws Exception 
     */
    public static String HashPassword(String text)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("SHA-1");

        byte[] sha1hash = new byte[40];
        byte[] data = text.getBytes("utf-8");
        md.update(data, 0, data.length);
        sha1hash = md.digest();
        return convertToHex(sha1hash);
    }

    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('A' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while (two_halfs++ < 1);
        }
        return buf.toString();
    }
}

Related

  1. SHA1(String text)
  2. dataDigest(String in)
  3. SHA1(String s)
  4. SHA1(String text)
  5. SHA256(String text)
  6. sha1(File input)
  7. sha1(String input)
  8. sha1(String ori)
  9. sha1(String ori)