Java SHA512 sha512(String string)

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

Description

sha

License

Apache License

Declaration

public static final String sha512(String string) 

Method Source Code


//package com.java2s;
/*//from  w w w .  j  a va2s  .  co  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.*;
import java.math.BigInteger;
import java.nio.file.Path;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static final String sha512(String string) {
        return calcHash(string, "SHA-512");
    }

    public static final String sha512(File file) throws FileNotFoundException, IOException {
        return calcHash(new FileInputStream(file), "SHA-512");
    }

    public static final String sha512(Path file) throws FileNotFoundException, IOException {
        return sha512(file.toFile());
    }

    public static final String sha512(InputStream inStream) throws IOException {
        return calcHash(inStream, "SHA-512");
    }

    public static final String sha512(byte[] bytes) {
        return calcHash(bytes, "SHA-512");
    }

    private static String calcHash(String string, String algorithm) {
        try {
            return calcHash(string.getBytes("UTF-8"), algorithm);
        } catch (UnsupportedEncodingException e) {
            return calcHash(string.getBytes(), algorithm);
        }
    }

    private static String calcHash(byte[] bytes, String algorithm) {
        try {
            MessageDigest m = MessageDigest.getInstance(algorithm);
            m.update(bytes);
            return new BigInteger(1, m.digest()).toString(16);
        } catch (NoSuchAlgorithmException ex) {
            // this should not happen
            return "";
        }
    }

    private static String calcHash(InputStream input, String algorithm) throws IOException {
        try {
            final MessageDigest md = MessageDigest.getInstance(algorithm);
            final DigestInputStream dis = new DigestInputStream(input, md);
            try {
                byte[] buff = new byte[4096];
                // just read to get the Digest filled...
                while (dis.read(buff) > 0) {
                    // nop
                }
                return new BigInteger(1, md.digest()).toString(16);
            } finally {
                dis.close();
            }
        } catch (NoSuchAlgorithmException e) {
            // this should not happen
            return "";
        }
    }
}

Related

  1. sha512(String input)
  2. SHA512(String original)
  3. sha512(String string)
  4. sha512AsBytes(byte[] input)
  5. sha512hash_base64(final String toHash)
  6. sha512String(String base)