package com.qst.dms.security;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class String_SHA_256
{
    public static String String_to_SHA256(String password)
    {
        StringBuilder hexString = new StringBuilder();
        if (password == null)
        {
            return null;
        }
        try {
            // 创建MessageDigest实例并指定算法为SHA-256
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            // 计算散列值
            byte[] hashBytes = digest.digest(password.getBytes());
            // 将散列值转换为十六进制字符串表示
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        finally
        {
            return hexString.toString();
        }
    }
}
