wkgcass / public-chat

My public repository for chat-in-issue (public repos do not have billing when executing Github Actions)
Creative Commons Zero v1.0 Universal
1 stars 0 forks source link

java sha256 #56

Open wkgcass opened 1 year ago

wkgcass commented 1 year ago

/chat: write a java program to calculate sha256 of a string

github-actions[bot] commented 1 year ago

/ai-says:

Sure! Here's a Java program that calculates the SHA-256 hash of a string:

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

public class SHA256Calculator {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        String inputString = "Hello, World!"; // Replace this with your own string

        // Create MessageDigest instance of SHA-256
        MessageDigest digest = MessageDigest.getInstance("SHA-256");

        // Apply SHA-256 hashing algorithm on the input string
        byte[] hashBytes = digest.digest(inputString.getBytes(StandardCharsets.UTF_8));

        // Convert byte array to hexadecimal string
        StringBuilder hexString = new StringBuilder();
        for (byte hashByte : hashBytes) {
            String hex = Integer.toHexString(0xff & hashByte);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }

        System.out.println("Input String: " + inputString);
        System.out.println("SHA-256 Hash: " + hexString.toString());
    }
}

In this program, we use the MessageDigest class from the java.security package to calculate the SHA-256 hash of a string. We convert the string to bytes using the UTF-8 encoding, apply the SHA-256 hashing algorithm, and store the result in a byte array. Finally, we convert the byte array to a hexadecimal string representation and print the result.

Make sure to replace Hello, World! with your own string in the inputString variable.

github-actions[bot] commented 1 year ago

/err: token usage: {"prompt_tokens":22,"completion_tokens":332,"total_tokens":354}