All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.azbh111.utils.java.digest.DigestUtils Maven / Gradle / Ivy

The newest version!
package com.github.azbh111.utils.java.digest;

import com.github.azbh111.utils.java.code.HexUtils;
import com.github.azbh111.utils.java.exception.ExceptionUtils;
import com.github.azbh111.utils.java.lang.io.CharSequenceInputStream;

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

/**
 * 摘要
 *
 * @author pyz
 * @date 2019/6/21 4:03 PM
 */
public class DigestUtils {

    public static byte[] digest(InputStream in, DigestAlgorithm algorithm) throws IOException {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm.getName());
            byte[] buf = new byte[1024];
            int n;
            while ((n = in.read(buf)) != -1) {
                md.update(buf, 0, n);
            }
            buf = md.digest();
            md.reset();
            return buf;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static String digestHexLowerCase(InputStream in, DigestAlgorithm algorithm) throws IOException {
        return HexUtils.hexLowerCaseEncode(digest(in, algorithm));
    }

    public static String digestHexUpperCase(InputStream in, DigestAlgorithm algorithm) throws IOException {
        return HexUtils.hexUpperCaseEncode(digest(in, algorithm));
    }

    public static byte[] digest(File file, DigestAlgorithm algorithm) throws IOException {
        return digest(wrap(file), algorithm);
    }

    public static String digestHexLowerCase(File in, DigestAlgorithm algorithm) throws IOException {
        return HexUtils.hexLowerCaseEncode(digest(in, algorithm));
    }

    public static String digestHexUpperCase(File in, DigestAlgorithm algorithm) throws IOException {
        return HexUtils.hexUpperCaseEncode(digest(in, algorithm));
    }

    public static byte[] digest(byte[] input, DigestAlgorithm algorithm) {
        try {
            return digest(wrap(input), algorithm);
        } catch (IOException e) {
            ExceptionUtils.throwException(e);
            return null;
        }
    }

    public static String digestHexLowerCase(byte[] in, DigestAlgorithm algorithm) {
        return HexUtils.hexLowerCaseEncode(digest(in, algorithm));
    }

    public static String digestHexUpperCase(byte[] in, DigestAlgorithm algorithm) {
        return HexUtils.hexUpperCaseEncode(digest(in, algorithm));
    }

    public static byte[] digest(CharSequence input, DigestAlgorithm algorithm) {
        try {
            return digest(wrap(input), algorithm);
        } catch (IOException e) {
            ExceptionUtils.throwException(e);
            return null;
        }
    }

    public static String digestHexLowerCase(CharSequence in, DigestAlgorithm algorithm) {
        return HexUtils.hexLowerCaseEncode(digest(in, algorithm));
    }

    public static String digestHexUpperCase(CharSequence in, DigestAlgorithm algorithm) {
        return HexUtils.hexUpperCaseEncode(digest(in, algorithm));
    }

    private static InputStream wrap(byte[] input) {
        return new ByteArrayInputStream(input);
    }

    private static InputStream wrap(CharSequence input) {
        return new CharSequenceInputStream(input, StandardCharsets.UTF_8, Math.min(Math.max(input.length() / 10, 128), 1024));
    }

    private static InputStream wrap(File input) throws FileNotFoundException {
        return new FileInputStream(input);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy