Get the bytes of the file and generate a hash

1

I am reading a txt file as follows, how would I do to save the bytes of the file and generate a sha-256 hash the class that generates I already own the code, as I do to save in an array of bytes or other suggestions. I'm sorry I'm a 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 that 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);
    
asked by anonymous 19.11.2017 / 05:51

0 answers