Take the bytes from the file and generate a hash

Asked

Viewed 379 times

1

Guys I am reading a txt file as follows, as I would to save the bytes of the file and generate a hash sha-256 the class it generates I also already own the code, as I do for storing in an array of bytes or other suggestions . I’m sorry I’m beginner in java

try {
            InputStream is = new FileInputStream("c://teste.txt");
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader reader = new BufferedReader(isr);
            String linha = reader.readLine();
            while (linha != null) {
                System.out.println(linha);
                linha = reader.readLine();
                }
                reader.close();
                } catch (IOException e) {
                System.out.println("Erro ao tentar ler o arquivo " + e);
                }

code I use to generate hash

 MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
 byte messageDigest[] = algorithm.digest(linha.getBytes("UTF-8"));

                             System.out.println(messageDigest);
                             StringBuilder hexString = new StringBuilder();
                             for (byte b : messageDigest) {
                               hexString.append(String.format("%02X", 0xFF & b));
                             }
                             String senhahex = hexString.toString();

                             System.out.println(senhahex);
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.