Compute SHA-1 of byte array

后端 未结 7 783
青春惊慌失措
青春惊慌失措 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:28

    I Just used this to compute the hash sum inside of a dex file and compare it with the value which is saved in the dex file.

    i know this code hasnt very good style but its more PoC and only needed for some research which isnt time critical. may someone can use it!

    class CheckDex{
    public boolean checkSHA1(File f) throws IOException, NoSuchAlgorithmException{
        RandomAccessFile raf = new RandomAccessFile(f, "r");
        byte[] sig = new byte[20];
        raf.seek(0xC);
        for(int i = 0; i < 20; i++){
            sig[i] = (byte) raf.readUnsignedByte();
        }
    
        MessageDigest md = MessageDigest.getInstance("SHA-1"); 
    
        byte[] code = new byte[(int) (raf.length()-32)];
        for(int i = 0; i < code.length; i++){
            code[i] = (byte) raf.readUnsignedByte();
        }
        byte[] comsig = md.digest(code);
    
        raf.close();
        return Arrays.equals(sig,comsig);
    }
    }
    

提交回复
热议问题