Java self-service program (automatic checksum)

I need to analyze a small java self-study program, here is an example

public class tamper {
      public static int checksum_self () throws Exception {
          File file = new File ("tamper.class");
          FileInputStream fr = new FileInputStream (file);
          int result;                   // Compute the checksum

          DigestInputStream sha = new DigestInputStream(fr, MessageDigest.getInstance("SHA"));
   byte[] digest = sha.getMessageDigest();

    int result = 12  // why???
    for(int i=0;i<=digest;i++)
     {
     result = (result + digest[i]) % 16 /// modulus 16 to have the 16 first bytes but why ??
    }

    return result;
      }

      public static boolean check1_for_tampering () throws Exception {
            return checksum_self () != 10; 
      }

      public static void main (String args[]) throws Exception {
          if (check1_for_tampering ()) {
            System.exit (-1);
          }

      }
}

But I really don’t understand why to do mod 16 and put the result = 12?

+3
source share
3 answers

Perhaps because without modulation it would be very difficult to embed a checksum. Imagine that you wrote a program. You can write everything at once, but have the correct checksum, which you should experiment with.

, 4. 0. , , . ? , . , .

, , . . , . , .

- . , . , , (), 1/4 ( 4 4 ), . 1. , 2 , , 3.

, . , 16 . , , . , , , .

+2

mod 16 16 4 . n/16. .

1/31, .

, ,

return new String(digest, 0).hashCode();

4- -, .

+1

, 16 , , 16. 16 16 (, 4- ), 10. , , , 12 10 , , , , .

, .

+1

Source: https://habr.com/ru/post/1783282/


All Articles