Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

In this page you can find the example usage for java.security MessageDigest getInstance.

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:br.com.bluesoft.pronto.service.Seguranca.java

public static String encrypt(final String x) {
    try {/*from  w  w w  .  j a va 2  s  . c  o  m*/
        final MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(x.getBytes());
        final byte[] hashMd5 = md.digest();
        final byte[] base64 = Base64.encodeBase64(hashMd5);
        return new String(base64);
    } catch (final Exception e) {
        return null;
    }
}

From source file:com.glaf.core.security.DigestUtil.java

public static void digestFile(String filename, String algorithm) {
    byte[] b = new byte[65536];
    int read = 0;
    FileInputStream fis = null;//from  w  ww. j  a  va 2s  .co m
    FileOutputStream fos = null;
    OutputStream encodedStream = null;
    try {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        fis = new FileInputStream(filename);
        while (fis.available() > 0) {
            read = fis.read(b);
            md.update(b, 0, read);
        }
        byte[] digest = md.digest();
        StringBuffer fileNameBuffer = new StringBuffer(256).append(filename).append('.').append(algorithm);
        fos = new FileOutputStream(fileNameBuffer.toString());
        encodedStream = MimeUtility.encode(fos, "base64");
        encodedStream.write(digest);
        fos.flush();
    } catch (Exception ex) {
        throw new RuntimeException("Error computing Digest: " + ex);
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(encodedStream);
    }
}

From source file:org.sharetask.utility.HashCodeUtil.java

public static String getShortHashCode(final String data) {
    try {//from ww w .  j a  v  a 2 s  .  c om
        final MessageDigest mda = MessageDigest.getInstance("SHA-1");
        final String baseSalt = String.valueOf(System.currentTimeMillis());
        final byte[] digest = mda.digest(baseSalt.getBytes(Charset.forName("UTF-8")));
        return new String(Hex.encode(digest));
    } catch (final NoSuchAlgorithmException e) {
        throw new UnsupportedOperationException(e);
    }
}

From source file:org.rivetlogic.utils.IntegrationUtils.java

public static String MD5(String str) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("MD5");

    byte[] dg = digest.digest(str.getBytes());

    BigInteger number = new BigInteger(1, dg);

    return number.toString(16);
}

From source file:BD.Encriptador.java

public static String Desencriptar(String textoEncriptado) throws Exception {

    String secretKey = "qualityinfosolutions"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {//from w w  w . j a va 2 s  .  c o  m
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:com.gwtcx.server.util.Security.java

public static String md5(final String text) {
    String hashword = null;//from  w ww . j a v  a  2 s  .  co  m

    try {
        final MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(text.getBytes());
        final BigInteger hash = new BigInteger(1, md5.digest());
        hashword = hash.toString(16);
    } catch (final NoSuchAlgorithmException nsae) {
    }

    while (hashword.length() < 32) {
        hashword = "0" + hashword;
    }

    return hashword;
}

From source file:com.algodefu.yeti.md5.MD5HashGenerator.java

public static String generateKeyByString(String string) {
    MessageDigest m = null;/*from   w  w w.  j  ava2 s.c  o m*/
    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.reset();
    m.update(string.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.toString(16);
    while (hashtext.length() < 32) {
        hashtext = "0" + hashtext;
    }
    return hashtext;
}

From source file:Conexion.newClass.java

public String encode(String texto) throws EncoderException {
    String secretKey = "mailEncrypted"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {//from   w  ww .  jav a 2 s.  c  o  m

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:ch.unibe.cde.geonet.kernel.security.Md5PasswordEncoder.java

@Override
public String encode(CharSequence cs) {
    try {/*from  ww  w.j av a  2  s .c  o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(cs.toString().getBytes("UTF-8"));
        return "md5:" + Md5PasswordEncoder.byteToHex(md.digest());
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
        Log.error(Log.JEEVES, ex);
    }
    return null;
}

From source file:com.enviosya.client.tool.Tool.java

public String Encriptar(String texto) {

    //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*from  w  w  w .  jav a  2  s .  c om*/

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (Exception e) {
        //Ac tengo que agregar el retorno de la exception
    }
    return base64EncryptedString;
}