Java SHA SHAsum(byte[] convertme, boolean prefix, boolean uppercase)

Here you can find the source of SHAsum(byte[] convertme, boolean prefix, boolean uppercase)

Description

SH Asum

License

Apache License

Declaration

public static String SHAsum(byte[] convertme, boolean prefix, boolean uppercase) 

Method Source Code


//package com.java2s;
/*//  w w  w.jav  a  2  s  .  com
 * Copyright 2014 scape.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public class Main {
    public static String SHAsum(byte[] convertme, boolean prefix, boolean uppercase) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            String sha1 = byteArray2Hex(md.digest(convertme));
            if (prefix) {
                return "sha1:" + sha1;
            } else {
                if (uppercase) {
                    return sha1.toUpperCase();
                } else {

                }
                return sha1;
            }
        } catch (NoSuchAlgorithmException e) {
            return "";
        }
    }

    private static String byteArray2Hex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        return formatter.toString();
    }
}

Related

  1. shaFile(String paramString)
  2. SHAHash(byte[] input)
  3. shaHash(String message)
  4. shaPsw(String inputText)
  5. SHAsum(byte[] convertme)
  6. SHAsum(byte[] input)