Java SHA512 sha512(String input)

Here you can find the source of sha512(String input)

Description

sha

License

Apache License

Parameter

Parameter Description
input a parameter

Exception

Parameter Description
NullPointerException an exception

Declaration

public static String sha512(String input) throws NullPointerException 

Method Source Code


//package com.java2s;
/*/*w  ww .  j a  va  2 s .c  o  m*/
 * Copyright 2004-2014 Aimluck,Inc.
 *
 * 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.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**
     * 
     * @param input
     * @return
     * @throws NullPointerException
     */
    public static String sha512(String input) throws NullPointerException {
        if (input == null) {
            throw new NullPointerException("The input parameter must not be null.");
        }
        return encrypt(input, "SHA-512");
    }

    /**
     * 
     * @param input
     * @param type
     * @return
     * @throws NullPointerException
     */
    public static String encrypt(String input, String type) throws NullPointerException {
        if (input == null) {
            throw new NullPointerException("The input parameter must not be null.");
        }
        if (type == null) {
            throw new NullPointerException("The type parameter must not be null.");
        }

        try {
            MessageDigest md = MessageDigest.getInstance(type);
            md.update(input.getBytes("MS932"));
            return toHexString(md.digest());
        } catch (NoSuchAlgorithmException e) {
            // ignore
        } catch (UnsupportedEncodingException e) {
            // ignore
        }
        return null;
    }

    /**
     * 
     * @param b
     * @return
     */
    private static String toHexString(byte[] b) {
        StringBuilder hexString = new StringBuilder();
        String plainText = null;
        for (int i = 0; i < b.length; i++) {
            plainText = Integer.toHexString(0xFF & b[i]);
            if (plainText.length() < 2) {
                plainText = "0" + plainText;
            }
            hexString.append(plainText);
        }
        return hexString.toString();
    }
}

Related

  1. SHA512(String original)
  2. sha512(String string)
  3. sha512(String string)
  4. sha512AsBytes(byte[] input)