bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
Hash.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.util;
2 
3 import java.security.MessageDigest;
4 import java.nio.charset.Charset;
5 import java.security.NoSuchAlgorithmException;
6 
7 public class Hash
8 {
12  private static final ThreadLocal<MessageDigest> md5hash = new ThreadLocal<MessageDigest>() {
13 
14  @Override
15  public MessageDigest initialValue()
16  {
17  try {
18  return MessageDigest.getInstance( "MD5" );
19  } catch ( NoSuchAlgorithmException e ) {
20  e.printStackTrace();
21  System.exit( 1 );
22  return null;
23  }
24  }
25  };
29  private static final ThreadLocal<MessageDigest> sha256hash = new ThreadLocal<MessageDigest>() {
30 
31  @Override
32  public MessageDigest initialValue()
33  {
34  try {
35  return MessageDigest.getInstance( "SHA-256" );
36  } catch ( NoSuchAlgorithmException e ) {
37  e.printStackTrace();
38  System.exit( 1 );
39  return null;
40  }
41  }
42  };
46  private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
50  private static final Charset UTF8 = Charset.forName( "UTF-8" );
51 
52  // MD5
53 
60  public static String md5( final byte[] bytes )
61  {
62  return toHexString( md5hash.get().digest( bytes ) );
63  }
64 
72  public static String md5( final String text )
73  {
74  return md5( text.getBytes( UTF8 ) );
75  }
76 
77  // SHA-256
78 
85  public static String sha256( final byte[] bytes )
86  {
87  return toHexString( sha256hash.get().digest( bytes ) );
88  }
89 
97  public static String sha256( final String text )
98  {
99  return sha256( text.getBytes( UTF8 ) );
100  }
101 
102  // Helper
103 
110  private static String toHexString( final byte[] bytes )
111  {
112  final char[] hexChars = new char[ bytes.length * 2 ];
113  for ( int j = 0; j < bytes.length; ++j ) {
114  final int v = bytes[j] & 0xFF;
115  hexChars[j * 2] = HEX_CHARS[v >>> 4];
116  hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
117  }
118  return new String( hexChars );
119  }
120 
121 }
static String md5(final byte[] bytes)
Compute md5 hash of given binary data.
Definition: Hash.java:60
static String sha256(final String text)
Compute sha256 hash of the given string.
Definition: Hash.java:97
static String sha256(final byte[] bytes)
Compute sha256 hash of given binary data.
Definition: Hash.java:85
static final Charset UTF8
Constant for the utf-8 charset, saves repeated lookups.
Definition: Hash.java:50
static final ThreadLocal< MessageDigest > sha256hash
Cache of sha256 digesters.
Definition: Hash.java:29
static String toHexString(final byte[] bytes)
Convert given binary data to hex.
Definition: Hash.java:110
static final ThreadLocal< MessageDigest > md5hash
Cache of md5 digesters.
Definition: Hash.java:12
static String md5(final String text)
Compute md5 hash of the given string.
Definition: Hash.java:72
static final char[] HEX_CHARS
For converting to hex string.
Definition: Hash.java:46