Compute SHA-1 of byte array

后端 未结 7 740
青春惊慌失措
青春惊慌失措 2020-11-29 23:02

I\'m looking for a way of getting an SHA-1 checksum with a Java byte array as the message.

Should I use a third party tool or is there something built in to the JVM

相关标签:
7条回答
  • 2020-11-29 23:44

    How about Using This:

    public class sha1Calculate {

        public static void main(String[] args)throws Exception
        {
             File file = new File("D:\\Android Links.txt");
            String outputTxt= "";
            String hashcode = null;
    
            try {
    
                FileInputStream input = new FileInputStream(file);
    
                ByteArrayOutputStream output = new ByteArrayOutputStream ();
                byte [] buffer = new byte [65536];
                int l;
    
                while ((l = input.read (buffer)) > 0)
                    output.write (buffer, 0, l);
    
                input.close ();
                output.close ();
    
                byte [] data = output.toByteArray ();
    
    
                    MessageDigest digest = MessageDigest.getInstance( "SHA-1" ); 
    
                byte[] bytes = data;
    
                digest.update(bytes, 0, bytes.length);
                bytes = digest.digest();
    
                StringBuilder sb = new StringBuilder();
    
                for( byte b : bytes )
                {
                    sb.append( String.format("%02X", b) );
                }
    
                    System.out.println("Digest(in hex format):: " + sb.toString());
    
    
            }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题