Java SHA1 generateSHA1(String message)

Here you can find the source of generateSHA1(String message)

Description

generate SHA

License

Open Source License

Declaration

public static String generateSHA1(String message) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;
import java.security.*;

public class Main {
    public static String generateSHA1(String message) {
        return hashString(message, "SHA-1");
    }/* w  w w .  j a va 2  s  .  c o  m*/

    private static String hashString(String message, String algorithm) {

        try {
            MessageDigest digest = MessageDigest.getInstance(algorithm);
            byte[] hashedBytes = digest.digest(message.getBytes("UTF-8"));

            return convertByteArrayToHexString(hashedBytes);
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
            return "problem";
        }
    }

    private static String convertByteArrayToHexString(byte[] arrayBytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < arrayBytes.length; i++) {
            stringBuffer.append(Integer.toString((arrayBytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        return stringBuffer.toString();
    }
}

Related

  1. digestSHA1(String login, String pass)
  2. digestSHA1(String text)
  3. digestSha1Hex(String source)
  4. doSHA1(byte[] buf)
  5. doSHA1(byte[] buf, int off, int len)
  6. generateSHA1ChecksumFile(String filename)
  7. generateSHA1Key(String input)
  8. generateSHA1RSASignature(RSAPrivateKey privKey, byte[] text)
  9. generateSHA1String(String stringToEncode)