登录
原创

JAVA进行SHA256加密

发布于 2025-01-22 阅读 178
  • 后端
  • Java
原创
import java.security.MessageDigest;  
import java.security.NoSuchAlgorithmException;  

public class SHA256Example {  
    public static void main(String[] args) {  
        String input = "Hello, World!";  
        String hashedOutput = hashWithSHA256(input);  
        System.out.println("SHA-256 Hash: " + hashedOutput);  
    }  

    public static String hashWithSHA256(String input) {  
        try {  
            MessageDigest digest = MessageDigest.getInstance("SHA-256");  
            byte[] hash = digest.digest(input.getBytes());  

            // Convert byte array to hexadecimal format  
            StringBuilder hexString = new StringBuilder();  
            for (byte b : hash) {  
                String hex = Integer.toHexString(0xff & b);  
                if (hex.length() == 1) hexString.append('0');  
                hexString.append(hex);  
            }  
            return hexString.toString();  
        } catch (NoSuchAlgorithmException e) {  
            throw new RuntimeException(e);  
        }  
    }  
} 

评论区

詹姆斯36岁了还能获得NBA总冠军,你呢?

0

0

4

举报