Class IntegrityChecker

java.lang.Object
com.craftingdead.protect.IntegrityChecker

public final class IntegrityChecker extends Object
Provides runtime integrity checking capabilities. Computes and verifies SHA-256 hashes of JAR files and resources. Now enhanced with native-level integrity checks when available. Falls back to pure Java implementation if native library is not loaded. All methods return booleans or null values instead of throwing exceptions to ensure protection checks never crash the application.
  • Method Details

    • computeJarHash

      public static String computeJarHash(Class<?> clazz)
      Computes the SHA-256 hash of the JAR file containing the given class.
      Parameters:
      clazz - The class whose containing JAR will be hashed
      Returns:
      Hex-encoded SHA-256 hash, or null if computation fails
    • computeResourceHash

      public static String computeResourceHash(String resourcePath, ClassLoader classLoader)
      Computes the SHA-256 hash of a resource inside the JAR.
      Parameters:
      resourcePath - The path to the resource (e.g., "META-INF/MANIFEST.MF")
      classLoader - The ClassLoader to use for loading the resource
      Returns:
      Hex-encoded SHA-256 hash, or null if computation fails
    • compareHashes

      public static boolean compareHashes(String hash1, String hash2)
      Compares two hash strings for equality.
      Parameters:
      hash1 - First hash
      hash2 - Second hash
      Returns:
      true if both hashes are non-null and equal (case-insensitive), false otherwise
    • verifyJarIntegrity

      public static boolean verifyJarIntegrity(Class<?> clazz, String expectedHash)
      Verifies the integrity of a class's containing JAR against an expected hash. TODO: In release mode, this should trigger stricter enforcement if verification fails.
      Parameters:
      clazz - The class to verify
      expectedHash - The expected SHA-256 hash (hex-encoded)
      Returns:
      true if the hash matches, false otherwise
    • verifyResourceIntegrity

      public static boolean verifyResourceIntegrity(String resourcePath, ClassLoader classLoader, String expectedHash)
      Verifies the integrity of a resource against an expected hash. TODO: In release mode, this should trigger stricter enforcement if verification fails.
      Parameters:
      resourcePath - The path to the resource
      classLoader - The ClassLoader to use
      expectedHash - The expected SHA-256 hash (hex-encoded)
      Returns:
      true if the hash matches, false otherwise
    • performComprehensiveIntegrityCheck

      public static boolean performComprehensiveIntegrityCheck(Class<?> clazz, String expectedHash)
      Performs comprehensive integrity check using both Java and native methods. In RELEASE mode with native library loaded: - Checks both Java SHA-256 hash AND native integrity verification - Both must pass for overall integrity to be confirmed In DEV mode or without native library: - Only performs Java-based checks - Logs advisory warnings but doesn't fail strictly
      Parameters:
      clazz - The class to verify
      expectedHash - The expected SHA-256 hash (can be null in dev mode)
      Returns:
      true if integrity checks pass, false otherwise
    • quickIntegrityCheck

      public static boolean quickIntegrityCheck()
      Quick integrity check that only uses native layer if available. Faster than comprehensive check, suitable for frequent validation.
      Returns:
      true if integrity appears OK, false if suspicious