Calculate String sha Hash - Android java.security

Android examples for java.security:Sha

Description

Calculate String sha Hash

Demo Code

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class Main{

    public static String sha1Hash(String input) {
        try {/*w  ww  .  ja v a 2  s  . c  o  m*/
            MessageDigest md = MessageDigest.getInstance("sha-1");
            md.update(input.getBytes("utf-8"));

            byte[] digest = md.digest();
            BigInteger bi = new BigInteger(1, digest);
            return String.format((Locale) null,
                    "%0" + (digest.length * 2) + "x", bi).toLowerCase(
                    Locale.ENGLISH);
        } catch (NoSuchAlgorithmException e) {
        } catch (UnsupportedEncodingException e) {
        }
        return input;
    }

}

Related Tutorials