zhanghaijian
2018-07-03 06580708bdc661873cbc2dfd6de8b3155f57b8ae
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
package ay.util;
 
 
import java.security.MessageDigest;
 
public class SecurityEncryptUtil {
    private static java.security.SecureRandom random = new java.security.SecureRandom();
    
 
    public static String md5(String source, int times) {
        String result = source;
        for (int i = 0; i < times; i++) {
            result = md5(source);
        }
        return result;
    }
 
    public static String sha1(String source, int times) {
        String result = source;
        for (int i = 0; i < times; i++) {
            result = sha1(source);
        }
        return result;
    }
 
    public static String sha256(String source, int times) {
        String result = source;
        for (int i = 0; i < times; i++) {
            result = sha256(source);
        }
        return result;
    }
 
    public static String sha384(String source, int times) {
        String result = source;
        for (int i = 0; i < times; i++) {
            result = sha384(source);
        }
        return result;
    }
 
    public static String sha512(String source, int times) {
        String result = source;
        for (int i = 0; i < times; i++) {
            result = sha512(source);
        }
        return result;
    }
 
    public static String md5(String source) {
        return hash("MD5", source);
    }
 
    public static String sha1(String source) {
        return hash("SHA-1", source);
    }
 
    public static String sha256(String source) {
        return hash("SHA-256", source);
    }
 
    public static String sha384(String source) {
        return hash("SHA-384", source);
    }
 
    public static String sha512(String source) {
        return hash("SHA-512", source);
    }
 
    public static String hash(String algorithm, String source) {
        try {
            StringBuilder result = new StringBuilder();
            MessageDigest md = MessageDigest.getInstance(algorithm);
            byte[] bytes = md.digest(source.getBytes("utf-8"));
            for (byte b : bytes) {
                String hex = Integer.toHexString(b & 0xFF);
                if (hex.length() == 1)
                    result.append("0");
                result.append(hex);
            }
            return result.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    private static String toHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1)
                result.append("0");
            result.append(hex);
        }
        return result.toString();
    }
 
    /**
     * md5 128bit 16bytes
     * <br/>
     * sha1 160bit 20bytes
     * <br/>
     * sha256 256bit 32bytes
     * <br/>
     * sha384 384bit 48bites
     * <br/>
     * sha512 512bit 64bites
     */
    public static String generateSalt(int numberOfBytes) {
        byte[] salt = new byte[numberOfBytes];
        random.nextBytes(salt);
        return toHex(salt);
    }
}