Here you can find the source of HashPassword(String text)
Parameter | Description |
---|---|
password | a parameter |
Parameter | Description |
---|---|
Exception | an exception |
public static String HashPassword(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
//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(); } }