加密算法比较:SHA1,SHA256,MD5

释放双眼,带上耳机,听听看~!

以一个60M的文件为测试样本,经过1000次的测试平均值,三种算法的表现为:


1
2
3
4
1MD5算法运行1000次的平均时间为:226ms
2SHA1算法运行1000次的平均时间为:308ms
3SHA256算法运行1000次的平均时间为:473ms
4

安全性方面,显然SHA256(又称SHA2)的安全性最高,但是耗时要比其他两种多很多。MD5相对较容易碰撞,因此,SHA1应该是这三种中性能最好的一款加密算法。

JAVA实现:


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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
1import java.io.File;
2import java.io.FileInputStream;
3import java.io.FileNotFoundException;
4import java.io.IOException;
5import java.nio.MappedByteBuffer;
6import java.nio.channels.FileChannel;
7import java.security.MessageDigest;
8import java.security.NoSuchAlgorithmException;
9import java.util.zip.CRC32;
10
11public class FileSafeCode {
12  /**
13   * 计算大文件 md5获取getMD5(); SHA1获取getSha1() CRC32获取 getCRC32()
14   */
15  protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
16          'f' };
17  public static MessageDigest messagedigest = null;
18
19 
20
21  /**
22   * 对一个文件获取md5值
23   *
24   * @return md5串
25   * @throws NoSuchAlgorithmException
26   */
27  public static String getMD5(File file) throws IOException, NoSuchAlgorithmException {
28
29      messagedigest = MessageDigest.getInstance("MD5");
30      FileInputStream in = new FileInputStream(file);
31      FileChannel ch = in.getChannel();
32      MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
33      messagedigest.update(byteBuffer);
34      return bufferToHex(messagedigest.digest());
35  }
36
37  /**
38   * // * @param target 字符串 求一个字符串的md5值 // * @return md5 value //
39   */
40  // public static String StringMD5(String target) {
41  // return DigestUtils.md5Hex(target);
42  // }
43
44  /***
45   * 计算SHA1码
46   *
47   * @return String 适用于上G大的文件
48   * @throws NoSuchAlgorithmException
49   */
50  public static String getSha1(File file) throws OutOfMemoryError, IOException, NoSuchAlgorithmException {
51      messagedigest = MessageDigest.getInstance("SHA-1");
52      FileInputStream in = new FileInputStream(file);
53      FileChannel ch = in.getChannel();
54      MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
55      messagedigest.update(byteBuffer);
56      return bufferToHex(messagedigest.digest());
57  }
58
59  /**
60   * 获取文件SHA256码
61   *
62   * @return String
63   */
64  public static String getSha256(File file) throws OutOfMemoryError, IOException, NoSuchAlgorithmException {
65      messagedigest = MessageDigest.getInstance("SHA-256");
66      FileInputStream in = new FileInputStream(file);
67      FileChannel ch = in.getChannel();
68      MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
69      messagedigest.update(byteBuffer);
70      return bufferToHex(messagedigest.digest());
71  }
72
73  /**
74   * 获取文件CRC32码
75   *
76   * @return String
77   */
78  public static String getCRC32(File file) {
79      CRC32 crc32 = new CRC32();
80      // MessageDigest.get
81      FileInputStream fileInputStream = null;
82      try {
83          fileInputStream = new FileInputStream(file);
84          byte[] buffer = new byte[8192];
85          int length;
86          while ((length = fileInputStream.read(buffer)) != -1) {
87              crc32.update(buffer, 0, length);
88          }
89          return crc32.getValue() + "";
90      } catch (FileNotFoundException e) {
91          e.printStackTrace();
92          return null;
93      } catch (IOException e) {
94          e.printStackTrace();
95          return null;
96      } finally {
97          try {
98              if (fileInputStream != null)
99                  fileInputStream.close();
100         } catch (IOException e) {
101             e.printStackTrace();
102         }
103     }
104 }
105
106 public static String getMD5String(String s) {
107     return getMD5String(s.getBytes());
108 }
109
110 public static String getMD5String(byte[] bytes) {
111     messagedigest.update(bytes);
112     return bufferToHex(messagedigest.digest());
113 }
114
115 /**
116  * @Description 计算二进制数据
117  * @return String
118  */
119 private static String bufferToHex(byte bytes[]) {
120     return bufferToHex(bytes, 0, bytes.length);
121 }
122
123 private static String bufferToHex(byte bytes[], int m, int n) {
124     StringBuffer stringbuffer = new StringBuffer(2 * n);
125     int k = m + n;
126     for (int l = m; l < k; l++) {
127         appendHexPair(bytes[l], stringbuffer);
128     }
129     return stringbuffer.toString();
130 }
131
132 private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
133     char c0 = hexDigits[(bt & 0xf0) >> 4];
134     char c1 = hexDigits[bt & 0xf];
135     stringbuffer.append(c0);
136     stringbuffer.append(c1);
137 }
138
139 public static boolean checkPassword(String password, String md5PwdStr) {
140     String s = getMD5String(password);
141     return s.equals(md5PwdStr);
142 }
143}
144

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

Bootstrap 间隔 (Spacing)

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索