1
2
3
4
5
6 package net.sf.mindoro.commons.util;
7
8 import java.security.MessageDigest;
9 import java.security.NoSuchAlgorithmException;
10
11 /***
12 * A utility class to calculate SHA-256 message digests.
13 *
14 * @author aisrael
15 */
16 public final class Sha256Util {
17
18 /***
19 * Instances of Sha256Util should NOT be constructed during regular
20 * programming.
21 */
22 private Sha256Util() {
23
24 }
25
26 /***
27 * Get the SHA-256 MessageDigest to use.
28 *
29 * @return MessageDigest
30 */
31 private static MessageDigest getDigest() {
32 try {
33 return MessageDigest.getInstance("SHA-256");
34 } catch (final NoSuchAlgorithmException e) {
35 throw new RuntimeException(e);
36 }
37 }
38
39 /***
40 * @param s
41 * String
42 * @return SHA-256 hash of s
43 */
44 public static String hash(final String s) {
45 return StringUtils.toHex(getDigest().digest(s.getBytes()));
46 }
47
48 }