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

io.lsn.spring.utilities.hash.Hash Maven / Gradle / Ivy

Go to download

Logisfera Nova Commons holds a bunch of useful helpers such as validators, conditional register process, soap logging handler

There is a newer version: 2.2.0
Show newest version
package io.lsn.spring.utilities.hash;


import io.lsn.logger.factory.LoggerFactory;
import io.lsn.logger.factory.logger.Logger;
import io.lsn.logger.factory.LoggerFactory;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Hash {

    private static final Logger logger = LoggerFactory.getLogger(Hash.class);

    public static String md5(String input) {
        return hash(input, "MD5");
    }

    public static String sha256(String input) {
        return hash(input, "SHA-256");
    }

    private static String hash(String input, String algorithm) {
        MessageDigest digest = null;
        if (null == input) {
            return null;
        }
        try {
            digest = MessageDigest.getInstance(algorithm);
            digest.update(input.getBytes(), 0, input.length());
        } catch (NoSuchAlgorithmException e) {
            logger.error(e.getMessage(), e);
        }
        assert digest != null;
        return new BigInteger(1, digest.digest()).toString(16);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy