Android SHA1 Hash Create SHA1(String s)

Here you can find the source of SHA1(String s)

Description

SHA

Declaration

public static String SHA1(String s) 

Method Source Code

//package com.java2s;
import java.security.MessageDigest;

public class Main {
    public static String SHA1(String s) {
        if (s == null)
            return "";
        try {/*from w  w  w.  j a  v  a2  s  .c o  m*/
            MessageDigest md;
            md = MessageDigest.getInstance("SHA-1");
            byte[] sha1hash = new byte[40];
            md.update(s.getBytes("iso-8859-1"), 0, s.length());
            sha1hash = md.digest();
            return convertToHex(sha1hash);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfByte = (data[i] >>> 4) & 0x0F;
            int twoHalves = 0;
            do {
                if ((0 <= halfByte) && (halfByte <= 9))
                    buf.append((char) ('0' + halfByte));
                else
                    buf.append((char) ('a' + (halfByte - 10)));
                halfByte = data[i] & 0x0F;
            } while (twoHalves++ < 1);
        }
        return buf.toString();
    }
}

Related

  1. SHA1(String str)
  2. sha512(String what_to_encode)
  3. SHA1(String text)
  4. dataDigest(String in)
  5. SHA1(String text)
  6. SHA256(String text)
  7. HashPassword(String text)
  8. sha1(File input)