summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/util/Hash.java
blob: 8db6a5f4305060a492bb96618518f3385bd175f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package org.openslx.imagemaster.util;

import java.security.MessageDigest;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;

public class Hash
{
	/**
	 * Cache of md5 digesters
	 */
	private static final ThreadLocal<MessageDigest> md5hash = new ThreadLocal<MessageDigest>() {

		@Override
		public MessageDigest initialValue()
		{
			try {
				return MessageDigest.getInstance( "MD5" );
			} catch ( NoSuchAlgorithmException e ) {
				e.printStackTrace();
				System.exit( 1 );
				return null;
			}
		}
	};
	/**
	 * Cache of sha256 digesters
	 */
	private static final ThreadLocal<MessageDigest> sha256hash = new ThreadLocal<MessageDigest>() {

		@Override
		public MessageDigest initialValue()
		{
			try {
				return MessageDigest.getInstance( "SHA-256" );
			} catch ( NoSuchAlgorithmException e ) {
				e.printStackTrace();
				System.exit( 1 );
				return null;
			}
		}
	};
	/**
	 * For converting to hex string
	 */
	private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	/**
	 * Constant for the utf-8 charset, saves repeated lookups
	 */
	private static final Charset UTF8 = Charset.forName( "UTF-8" );

	// MD5

	/**
	 * Compute md5 hash of given binary data.
	 * 
	 * @param bytes the binary data in a byte array
	 * @return hex representation of the 128bit md5 hash
	 */
	public static String md5( final byte[] bytes )
	{
		return toHexString( md5hash.get().digest( bytes ) );
	}

	/**
	 * Compute md5 hash of the given string.
	 * The string will be converted to utf-8 before computation.
	 * 
	 * @param text the text to hash
	 * @return hex representation of the 128bit md5 hash
	 */
	public static String md5( final String text )
	{
		return md5( text.getBytes( UTF8 ) );
	}

	// SHA-256

	/**
	 * Compute sha256 hash of given binary data.
	 * 
	 * @param bytes the binary data in a byte array
	 * @return hex representation of the 256bit sha256 hash
	 */
	public static String sha256( final byte[] bytes )
	{
		return toHexString( sha256hash.get().digest( bytes ) );
	}

	/**
	 * Compute sha256 hash of the given string.
	 * The string will be converted to utf-8 before computation.
	 * 
	 * @param text the text to hash
	 * @return hex representation of the 256bit sha256 hash
	 */
	public static String sha256( final String text )
	{
		return sha256( text.getBytes( UTF8 ) );
	}

	// Helper

	/**
	 * Convert given binary data to hex.
	 * 
	 * @param bytes binary data in a byte array
	 * @return upper case hex representation of bytes
	 */
	private static String toHexString( final byte[] bytes )
	{
		final char[] hexChars = new char[ bytes.length * 2 ];
		for ( int j = 0; j < bytes.length; ++j ) {
			final int v = bytes[j] & 0xFF;
			hexChars[j * 2] = HEX_CHARS[v >>> 4];
			hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
		}
		return new String( hexChars );
	}

}