-
Notifications
You must be signed in to change notification settings - Fork 4
/
EncryptionUtils.java
executable file
·174 lines (160 loc) · 4.76 KB
/
EncryptionUtils.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.bytesizebit.androidutils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/***********
* Android Utils
* Created by Shahar Barsheshet on 20/01/2016.
* bytesizebit@gmail.com
* www.bytesizebit.com
***********/
public class EncryptionUtils {
private EncryptionUtils() {
}
/**
* Create MD5 String from string
*
* @param str the string
* @return MD5 String
*/
public static String getMD5String(String str) {
return getMD5String(str.getBytes());
}
/**
* Create MD5 String with salt
*
* @param str the string
* @param salt salt
* @return MD5 String
*/
public static String getMD5String(String str, String salt) {
return bytesToHex(encryptMD5((str + salt).getBytes()));
}
/**
* Create MD5 String from byte array
*
* @param bytes byte array
* @return MD5 String
*/
public static String getMD5String(byte[] bytes) {
return bytesToHex(encryptMD5(bytes));
}
/**
* Create MD5 String from byte array with salt
*
* @param bytes byte array
* @param salt salt
* @return MD5 String
*/
public static String getMD5String(byte[] bytes, byte[] salt) {
byte[] dataSalt = new byte[bytes.length + salt.length];
System.arraycopy(bytes, 0, dataSalt, 0, bytes.length);
System.arraycopy(salt, 0, dataSalt, bytes.length, salt.length);
return bytesToHex(encryptMD5(dataSalt));
}
/**
* Encrypt byte array
*
* @param bytes bytes to encrypt
* @return encrypted byte array if available
*/
public static byte[] encryptMD5(byte[] bytes) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes);
return md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return new byte[0];
}
/**
* Create a MD5 String from file path
*
* @param filePath path to a file
* @return {@code String} MD5 string
*/
public static String getMD5File(String filePath) {
return getMD5File(new File(filePath));
}
/**
* Create a MD5 String from file
*
* @param file file to get MD5 from
* @return MD5 String
*/
public static String getMD5File(File file) {
FileInputStream in = null;
try {
in = new FileInputStream(file);
FileChannel channel = in.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buffer);
return bytesToHex(md.digest());
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
}
}
}
return "";
}
/**
* Get SHA1 from String
*
* @param str String to get SHA1 from
* @return SHA1 string
*/
public static String getSHA(String str) {
return getSHA(str.getBytes());
}
/**
* Get SHA1 from byte array
*
* @param bytes bytes to get SHA1 from
* @return SHA1 String
*/
public static String getSHA(byte[] bytes) {
return bytesToHex(encryptSHA(bytes));
}
/**
* Encrypt SHA1
*
* @param bytes bytes to encrypt
* @return an SHA1 encrypted byte array
*/
public static byte[] encryptSHA(byte[] bytes) {
try {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(bytes);
return md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return new byte[0];
}
/**
* Convert bytes to Hex
*
* @param src byte array to convert
* @return String from converted byte array
*/
public static String bytesToHex(byte[] src) {
char[] res = new char[src.length * 2];
final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
for (int i = 0, j = 0; i < src.length; i++) {
res[j++] = hexDigits[src[i] >>> 4 & 0x0f];
res[j++] = hexDigits[src[i] & 0x0f];
}
return new String(res);
}
}