java/nekurak.net-lib/src/cz/frantovo/nekurak/util/Hash.java
changeset 145 0efefbf5f8b6
parent 123 9135d52e8b0a
     1.1 --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/util/Hash.java	Tue May 25 13:54:21 2010 +0200
     1.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/util/Hash.java	Sun Jun 20 14:46:47 2010 +0200
     1.3 @@ -10,39 +10,39 @@
     1.4   */
     1.5  public class Hash {
     1.6  
     1.7 -    private static String algoritmus = "SHA-512";
     1.8 -    private static String kodovani = "UTF-8";
     1.9 +	private static String algoritmus = "SHA-512";
    1.10 +	private static String kodovani = "UTF-8";
    1.11  
    1.12 -    private static String prevedNaHex(byte[] data) {
    1.13 -	StringBuffer vysledek = new StringBuffer();
    1.14 -	for (int i = 0; i < data.length; i++) {
    1.15 -	    int pulBajt = (data[i] >>> 4) & 0x0F;
    1.16 -	    int dvePulky = 0;
    1.17 -	    do {
    1.18 -		if ((0 <= pulBajt) && (pulBajt <= 9)) {
    1.19 -		    vysledek.append((char) ('0' + pulBajt));
    1.20 -		} else {
    1.21 -		    vysledek.append((char) ('a' + (pulBajt - 10)));
    1.22 +	private static String prevedNaHex(byte[] data) {
    1.23 +		StringBuilder vysledek = new StringBuilder();
    1.24 +		for (int i = 0; i < data.length; i++) {
    1.25 +			int pulBajt = (data[i] >>> 4) & 0x0F;
    1.26 +			int dvePulky = 0;
    1.27 +			do {
    1.28 +				if ((0 <= pulBajt) && (pulBajt <= 9)) {
    1.29 +					vysledek.append((char) ('0' + pulBajt));
    1.30 +				} else {
    1.31 +					vysledek.append((char) ('a' + (pulBajt - 10)));
    1.32 +				}
    1.33 +				pulBajt = data[i] & 0x0F;
    1.34 +			} while (dvePulky++ < 1);
    1.35  		}
    1.36 -		pulBajt = data[i] & 0x0F;
    1.37 -	    } while (dvePulky++ < 1);
    1.38 +		return vysledek.toString();
    1.39  	}
    1.40 -	return vysledek.toString();
    1.41 -    }
    1.42  
    1.43 -    /**
    1.44 -     * @param text vstupní text
    1.45 -     * @return hashovaný text v HEX tvaru
    1.46 -     * @throws NoSuchAlgorithmException neexistující hashovací algoritmus
    1.47 -     * @throws UnsupportedEncodingException nepodporované kódování znaků
    1.48 -     * @throws NullPointerException pokud je vstup null
    1.49 -     */
    1.50 -    public static String hashuj(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    1.51 -	MessageDigest md;
    1.52 -	md = MessageDigest.getInstance(algoritmus);
    1.53 -	byte[] hash = new byte[40];
    1.54 -	md.update(text.getBytes(kodovani), 0, text.length());
    1.55 -	hash = md.digest();
    1.56 -	return prevedNaHex(hash);
    1.57 -    }
    1.58 +	/**
    1.59 +	 * @param text vstupní text
    1.60 +	 * @return hashovaný text v HEX tvaru
    1.61 +	 * @throws NoSuchAlgorithmException neexistující hashovací algoritmus
    1.62 +	 * @throws UnsupportedEncodingException nepodporované kódování znaků
    1.63 +	 * @throws NullPointerException pokud je vstup null
    1.64 +	 */
    1.65 +	public static String hashuj(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    1.66 +		MessageDigest md;
    1.67 +		md = MessageDigest.getInstance(algoritmus);
    1.68 +		byte[] hash = new byte[40];
    1.69 +		md.update(text.getBytes(kodovani), 0, text.length());
    1.70 +		hash = md.digest();
    1.71 +		return prevedNaHex(hash);
    1.72 +	}
    1.73  }