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:edu.mit.media.funf.HashUtil.java

public static MessageDigest getMessageDigest() {
    if (instance == null) {
        try {//from   w w  w .  j  a  v  a 2 s.c  o  m
            instance = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "HashUtil no SHA alghrithom", e);
            return null;
        }
    }
    return instance;
}

From source file:ca.ualberta.physics.cssdp.util.HashUtils.java

/**
 * From a password, a number of iterations and a salt, returns the
 * corresponding digest//from  w  ww  .j ava2s  .  c o m
 * 
 * @param iterationNb
 *            int The number of iterations of the algorithm
 * @param password
 *            String The password to encrypt
 * @param salt
 *            byte[] The salt
 * @return byte[] The digested password
 * @throws NoSuchAlgorithmException
 *             If the algorithm doesn't exist
 */
public static byte[] getHash(int iterationNb, String password, byte[] salt) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(salt);
        byte[] input = digest.digest(password.getBytes("UTF-8"));
        for (int i = 0; i < iterationNb; i++) {
            digest.reset();
            input = digest.digest(input);
        }
        return input;
    } catch (NoSuchAlgorithmException nsa) {
        throw Throwables.propagate(nsa);
    } catch (UnsupportedEncodingException e) {
        throw Throwables.propagate(e);
    }
}

From source file:controlpac.EncryptHelper.java

public static String Desencriptar(String textoEncriptado) {
    String base64EncryptedString = "";
    try {/* ww  w . j av a2s. co m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));//Desencodea el texto en base64
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); //Crea un hash con la clave elegida
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");//Crea la clave 
        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key); //Inicia el descifrado
        byte[] plainText = decipher.doFinal(message);//Descifra el texto
        base64EncryptedString = new String(plainText, "UTF-8");
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:$.Digests.java

/**
     * , ?md5sha1.//  ww w . ja  v  a2  s.  c o  m
     */
    private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
        try {
            MessageDigest digest = MessageDigest.getInstance(algorithm);

            if (salt != null) {
                digest.update(salt);
            }

            byte[] result = digest.digest(input);

            for (int i = 1; i < iterations; i++) {
                digest.reset();
                result = digest.digest(result);
            }
            return result;
        } catch (GeneralSecurityException e) {
            throw Exceptions.unchecked(e);
        }
    }

From source file:com.github.feribg.audiogetter.helpers.Utils.java

public static String calculateMD5(File file) {
    MessageDigest digest;/*  w  w  w  . j a  va2  s .c  o m*/
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.e(App.TAG, "Exception while getting Digest", e);
        return null;
    }

    InputStream is;
    try {
        is = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        Log.e(App.TAG, "Exception while getting FileInputStream", e);
        return null;
    }

    byte[] buffer = new byte[8192];
    int read;
    try {
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        String output = bigInt.toString(16);
        // Fill to 32 chars
        output = String.format("%32s", output).replace(' ', '0');
        return output;
    } catch (IOException e) {
        //throw new RuntimeException("Unable to process file for MD5", e);
        Log.e(App.TAG, "Unable to process file for MD5", e); //TODO check if actually avoid FC
        return "00000000000000000000000000000000"; // fictional bad MD5: needed without "throw new RuntimeException"
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(App.TAG, "Exception on closing MD5 input stream", e);
        }
    }
}

From source file:inventory.pl.helpers.Encryptor.java

public synchronized String encrypt(String plaintext, boolean useXor) {
    if (useXor)/*from  ww  w  .  j a  v a  2s .com*/
        plaintext = getXoredString(plaintext);
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256"); //step 2
    } catch (NoSuchAlgorithmException e) {

    }
    try {
        md.update(plaintext.getBytes("UTF-8")); //step 3
    } catch (UnsupportedEncodingException e) {

    }

    byte raw[] = md.digest(); //step 4
    String hash = Base64.encodeBase64String(raw); //step 5
    return hash; //step 6
}

From source file:net.longfalcon.newsj.util.EncodingUtil.java

public static String md5Hash(String input) {
    if (_md5Digest == null) {
        try {/*w ww.  java2 s .c o m*/
            _md5Digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            // this will not fail.
            e.printStackTrace();
            return null;
        }
    }

    byte[] digest = _md5Digest.digest(input.getBytes());

    StringBuilder sb = new StringBuilder();

    for (byte b : digest) {
        sb.append(String.format("%02x", b));
    }

    return sb.toString();
}

From source file:com.roncoo.pay.permission.utils.RonCooSignUtil.java

/**
 * @param timeStamp//from w w w. j  a v  a  2 s  .c  o m
 * @param userName
 * @param userPwd
 * @return
 */
public static String getSign(String token, long timeStamp, String userName) {

    String[] arr = new String[] { token, String.valueOf(timeStamp), userName };
    // token?timestamp?nonce?userPwd???
    Arrays.sort(arr);
    StringBuilder content = new StringBuilder();
    for (int i = 0; i < arr.length; i++) {
        content.append(arr[i]);
    }
    MessageDigest md = null;
    String tmpStr = null;

    try {
        md = MessageDigest.getInstance("SHA-1");
        // ??sha1
        byte[] digest = md.digest(content.toString().getBytes());
        tmpStr = byteToStr(digest);
    } catch (NoSuchAlgorithmException e) {
        LOG.error(e);
    }
    return tmpStr;
}

From source file:com.zotoh.maedr.device.netty.NettpHplr.java

/**
 * @param key//from   w  w  w. ja va 2s.  c o  m
 * @return
 */
public static String calcHybiSecKeyAccept(String key) {
    // add fix GUID according to 
    // http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10
    String k = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    String rc = "";
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] bits = md.digest(k.getBytes("utf-8"));
        rc = Base64.encodeBase64String(bits);
    } catch (Exception e) {
        //TODO
    }
    return rc;
}

From source file:me.j360.dubbo.modules.util.text.HashUtil.java

private static ThreadLocal<MessageDigest> createThreadLocalMessageDigest(final String digest) {
    return new ThreadLocal<MessageDigest>() {
        @Override/*from   w  w  w.ja va 2  s  . c o m*/
        protected MessageDigest initialValue() {
            try {
                return MessageDigest.getInstance(digest);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(
                        "unexpected exception creating MessageDigest instance for [" + digest + "]", e);
            }
        }
    };
}