-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.java
More file actions
33 lines (28 loc) · 1.08 KB
/
Copy pathHash.java
File metadata and controls
33 lines (28 loc) · 1.08 KB
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
package com.proyecto.miconsultorio.Security;
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");
}
}