Android SHA1 Hash Create sha1(String s)

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

Description

SHA1 algorithm

Declaration

public static String sha1(String s) 

Method Source Code

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

public class Main {
    /**//from  w w  w.  ja  va 2 s  .c o  m
     * SHA1 algorithm
     */
    public static String sha1(String s) {
        return encrypt(s, "SHA-1");
    }

    /**
     * encrypt method
     * 
     * @param s
     * @param method
     *            encrypt type
     * @return
     */
    private static String encrypt(String s, String method) {
        try {
            MessageDigest digest = MessageDigest.getInstance(method);
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();
            return toHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * to hex string
     * @param keyData
     * @return
     */
    private static String toHexString(byte[] keyData) {
        if (keyData == null) {
            return null;
        }
        int expectedStringLen = keyData.length * 2;
        StringBuilder sb = new StringBuilder(expectedStringLen);
        for (int i = 0; i < keyData.length; i++) {
            String hexStr = Integer.toString(keyData[i] & 0x00FF, 16);
            if (hexStr.length() == 1) {
                hexStr = "0" + hexStr;
            }
            sb.append(hexStr);
        }
        return sb.toString();

    }
}

Related

  1. HashPassword(String text)
  2. sha1(File input)
  3. sha1(String input)
  4. sha1(String ori)
  5. sha1(String ori)
  6. sha1(String s)
  7. computeHashSHA1(final String text)
  8. computeSHAHash(String password)
  9. getSHA1CertFingerprint(Context ctx)