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

io.castled.utils.CastledEncryptionUtils Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package io.castled.utils;

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

public class CastledEncryptionUtils {

    public static String toSHA256String(String str) {
        MessageDigest digest = getSHA256MessageDigest();
        byte[] hash = digest.digest(str.getBytes(StandardCharsets.UTF_8));
        StringBuilder result = new StringBuilder();
        for (byte b : hash) {
            result.append(String.format("%02x", b));
        }
        return result.toString();
    }

    public static MessageDigest getSHA256MessageDigest() {
        try {
            return MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Missing SHA-256 algorithm implementation.", e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy