Java SHA512 sha512(String string)

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

Description

Hashes the given string using the SHA-512 algorithm.

License

Open Source License

Declaration

public static byte[] sha512(String string) 

Method Source Code


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

import java.io.UnsupportedEncodingException;

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

public class Main {
    /** Hashes the given {@code string} using the SHA-512 algorithm. */
    public static byte[] sha512(String string) {
        return hash("SHA-512", string);
    }//from w w w  .j  a v a 2  s .  c  o  m

    /** Hashes the given {@code string} using the given {@code algorithm}. */
    public static byte[] hash(String algorithm, String string) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException ex) {
            throw new IllegalArgumentException(String.format("[%s] isn't a valid hash algorithm!", algorithm), ex);
        }

        byte[] bytes;
        try {
            bytes = string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new IllegalStateException(ex);
        }

        return digest.digest(bytes);
    }
}

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)