-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encryption.java
88 lines (81 loc) · 2.15 KB
/
Encryption.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
package server.model;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Encryption{
public Encryption(){
// Constructor
}
public String getHash(int hash, String var){
// 0 - SHA1, 1 - MD5, 2 - SHA256, 3 - SHA512
switch(hash){
case 0:
return getSha1(var);
case 1:
return getMd5(var);
case 2:
return getSha256(var);
case 3:
return getSha512(var);
default:
return getSha1(var);
}
}
public String getSha1(String var){
MessageDigest md = null;
try{
md = MessageDigest.getInstance("SHA1");
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}
md.reset();
byte buffer[] = var.getBytes();
md.update(buffer);
byte digest[] = md.digest();
String hash = "";
for(int i = 0; i < digest.length; i++)
hash += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
return hash;
}
public String getMd5(String var){
byte[] defaultBytes = var.getBytes();
MessageDigest md = null;
try{
md = MessageDigest.getInstance("MD5");
}catch(Exception e){
e.printStackTrace();
}
md.reset();
md.update(defaultBytes);
byte messageDigest[] = md.digest();
StringBuffer hexString = new StringBuffer();
for(int i = 0; i < messageDigest.length; i++)
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
return hexString.toString();
}
public String getSha256(String var){
MessageDigest md = null;
try{
md = MessageDigest.getInstance("SHA-256");
}catch(Exception e){
e.printStackTrace();
}
byte hash[] = md.digest(var.getBytes());
StringBuffer hexString = new StringBuffer();
for(byte b : hash)
hexString.append(String.format("%02x", b));
return hexString.toString();
}
public String getSha512(String var){
MessageDigest md = null;
try{
md = MessageDigest.getInstance("SHA-256");
}catch(Exception e){
e.printStackTrace();
}
byte hash[] = md.digest(var.getBytes());
StringBuffer hexString = new StringBuffer();
for(byte b : hash)
hexString.append(String.format("%02x", b));
return hexString.toString();
}
}