-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.java
32 lines (26 loc) · 1.03 KB
/
hash.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
package HackatonInicioUsuario;
public class hash {
/* Retorna un hash a partir de un tipo y un texto */
public static String getHash(String txt, String hashType) {
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance(hashType);
byte[] array = md.digest(txt.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
System.out.println(e.getMessage());
}
return null;
}
/* Retorna un hash MD5 a partir de un texto */
public static String md5(String txt) {
return hash.getHash(txt, "MD5");
}
/* Retorna un hash SHA1 a partir de un texto */
public static String sha1(String txt) {
return hash.getHash(txt, "SHA1");
}
}