Java SHA1 sha1(Object object)

Here you can find the source of sha1(Object object)

Description

Originally from http://www.velocityreviews.com/forums/t131917-sha1-hash-generator-in-hex.html

License

Open Source License

Parameter

Parameter Description
object a parameter

Declaration

public static String sha1(Object object) throws Exception 

Method Source Code

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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**// ww  w. j ava2  s .co  m
     * Originally from
     * http://www.velocityreviews.com/forums/t131917-sha1-hash-generator-in-hex.html
     *
     * @param object
     * @return
     */
    public static String sha1(Object object) throws Exception {
        if (object == null) {
            throw new Exception("Object is null.");
        }

        String input = String.valueOf(object);

        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException ex) {
            return null;
        }
        md.reset();

        byte[] buffer = input.getBytes();
        md.update(buffer);

        byte[] digest = md.digest();
        String hexStr = "";
        for (int i = 0; i < digest.length; i++) {
            hexStr += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
        }
        return hexStr;
    }
}

Related

  1. SHA1(final String text)
  2. sha1(final String text)
  3. sha1(final String todigest)
  4. SHA1(InputStream in)
  5. SHA1(MessageDigest messageDigest, String... texts)
  6. sha1(String data)
  7. sha1(String data)
  8. sha1(String data)
  9. sha1(String data)