def __init__(self, alg=None): if alg is not None: self.name = alg self.__hash = MessageDigest.getInstance(alg)
def calc_md5(text): md5 = MessageDigest.getInstance("md5") md5.update(text) return jarray_to_hex_s(md5.digest()) #Aqui reside la unica complejidad
import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Lab12 { static String hashString(String message, String algorithm) { // compute the hash value of message using algorithm and return a string representation of it byte[] hashedBytes = null; // will store the hash value of message try { // instantiate the specified algorithm // it may not exist, thus the try-catch MessageDigest digest = MessageDigest.getInstance(algorithm); // compute the hash value of the message hashedBytes = digest.digest(message.getBytes("UTF-8")); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { e. printStackTrace(); } // convert hash value (in byte[]) to a hex String and return result return bToH(hashedBytes); } // hashString() static String bToH(byte[] value) { // converts value to a String of hex digits StringBuilder sb = new StringBuilder(value.length*2);