Java SHA1 sha1Hash(String content)

Here you can find the source of sha1Hash(String content)

Description

sha Hash

License

Open Source License

Declaration

public static String sha1Hash(String content) 

Method Source Code


//package com.java2s;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String sha1Hash(String content) {
        byte[] bytesOfMessage = null;
        byte[] theDigest = null;

        try {//w  w w . j  a v  a2s  .  co m
            bytesOfMessage = content.getBytes("UTF-8");

            MessageDigest md = MessageDigest.getInstance("SHA-1");
            theDigest = md.digest(bytesOfMessage);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return theDigest == null ? null : convertDigestToString(theDigest);
    }

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

        return result.toString();
    }
}

Related

  1. sha1hash(Collection nquads)
  2. sha1hash(File f)
  3. sha1Hash(File file)
  4. sha1Hash(final String tohash)
  5. sha1Hash(InputStream in)
  6. sha1Hash(String input)
  7. sha1Hash(String input)
  8. sha1hash(String key)
  9. sha1Hash(String s)