Roberto Sánchez
2014-02-19 35905fe25b644bee5e213534887e10672ed58000
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
package net.curisit.securis.utils;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LicUtils {
   private static final Logger log = LoggerFactory.getLogger(LicUtils.class);
   public static String md5(String str) {
       try {
           MessageDigest mDigest = MessageDigest.getInstance("MD5");
           mDigest.update(str.getBytes(), 0, str.length());
           BigInteger i = new BigInteger(1, mDigest.digest());
           return String.format("%1$032x", i);
       } catch (NoSuchAlgorithmException e) {
           log.error("Error generating MD5 for string: " + str, e);
       }
       return null;
   }
   public static String sha256(String str) {
       return sha256(str.getBytes());
   }
   public static String sha256(byte[] bytes) {
       try {
           MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
           mDigest.update(bytes, 0, bytes.length);
           BigInteger i = new BigInteger(1, mDigest.digest());
           return String.format("%1$064x", i);
       } catch (NoSuchAlgorithmException e) {
           log.error("Error generating SHA-256 for bytes: " + bytes, e);
       }
       return null;
   }
   public static String sha256(byte[]... bytes) {
       try {
           MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
           for (byte[] bs : bytes) {
               mDigest.update(bs, 0, bs.length);
           }
           BigInteger i = new BigInteger(1, mDigest.digest());
           return String.format("%1$064x", i);
       } catch (NoSuchAlgorithmException e) {
           log.error("Error generating SHA-256 for bytes: " + bytes, e);
       }
       return null;
   }
}