Java Digest digest(String base)

Here you can find the source of digest(String base)

Description

digest

License

LGPL

Declaration

public static String digest(String base) 

Method Source Code


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

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String digest(String base) {
        MessageDigest digest = defaultMessageDigest();
        byte[] hash = digest.digest(base.getBytes(StandardCharsets.UTF_8));
        return bytesToHexString(hash);
    }/* w  ww  .j a v a2  s  .c o  m*/

    public static MessageDigest defaultMessageDigest() {
        try {
            return MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();

        for (byte aHash : bytes) {
            String hex = Integer.toHexString(0xff & aHash);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }

        return hexString.toString();
    }
}

Related

  1. digest(String alg, byte[] plainByte)
  2. digest(String algorithm, byte[] bytes)
  3. digest(String algorithm, byte[] data)
  4. digest(String algorithm, String data)
  5. digest(String aValue)
  6. digest(String digest, byte[] data)
  7. digest(String ha1, String ha2, String nonce)
  8. digest(String input, Charset charset)
  9. digest(String input, String algorithm, String encoding)