Java SHA1 SHA1(MessageDigest messageDigest, String... texts)

Here you can find the source of SHA1(MessageDigest messageDigest, String... texts)

Description

SHA

License

Apache License

Declaration

public static String SHA1(MessageDigest messageDigest, String... texts) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.security.MessageDigest;

public class Main {
    private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    public static String SHA1(MessageDigest messageDigest, String... texts) throws Exception {
        try {/*  w  w w. j a v a 2s  .  co  m*/
            if (messageDigest == null) {
                throw new Exception("messageDigest is null or text is null");
            }
            if (texts == null || texts.length == 0)
                return null;
            if (texts[0] == null)
                return null;
            messageDigest.reset();
            for (String tt : texts) {
                if (tt != null)
                    messageDigest.update(tt.getBytes());
            }

            byte[] updateBytes = messageDigest.digest();
            int len = updateBytes.length;
            char myChar[] = new char[len * 2];
            int k = 0;
            for (int i = 0; i < len; i++) {
                byte byte0 = updateBytes[i];
                myChar[k++] = hexDigits[byte0 >>> 4 & 0x0f];
                myChar[k++] = hexDigits[byte0 & 0x0f];
            }
            return new String(myChar);
        } catch (Exception e) {
            throw e;
        }
    }

    public static String SHA1(String... texts) throws Exception {
        try {

            if (texts == null || texts.length == 0)
                return null;
            if (texts[0] == null)
                return null;

            MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
            for (String tt : texts) {
                if (tt != null)
                    messageDigest.update(tt.getBytes());
            }
            byte[] updateBytes = messageDigest.digest();
            int len = updateBytes.length;
            char myChar[] = new char[len * 2];
            int k = 0;
            for (int i = 0; i < len; i++) {
                byte byte0 = updateBytes[i];
                myChar[k++] = hexDigits[byte0 >>> 4 & 0x0f];
                myChar[k++] = hexDigits[byte0 & 0x0f];
            }
            return new String(myChar);
        } catch (Exception e) {
            throw e;
        }
    }
}

Related

  1. sha1(final String string)
  2. SHA1(final String text)
  3. sha1(final String text)
  4. sha1(final String todigest)
  5. SHA1(InputStream in)
  6. sha1(Object object)
  7. sha1(String data)
  8. sha1(String data)
  9. sha1(String data)