Package com.craftingdead.protect
Class IntegrityChecker
java.lang.Object
com.craftingdead.protect.IntegrityChecker
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 Summary
Modifier and TypeMethodDescriptionstatic booleancompareHashes(String hash1, String hash2) Compares two hash strings for equality.static StringcomputeJarHash(Class<?> clazz) Computes the SHA-256 hash of the JAR file containing the given class.static StringcomputeResourceHash(String resourcePath, ClassLoader classLoader) Computes the SHA-256 hash of a resource inside the JAR.static booleanperformComprehensiveIntegrityCheck(Class<?> clazz, String expectedHash) Performs comprehensive integrity check using both Java and native methods.static booleanQuick integrity check that only uses native layer if available.static booleanverifyJarIntegrity(Class<?> clazz, String expectedHash) Verifies the integrity of a class's containing JAR against an expected hash.static booleanverifyResourceIntegrity(String resourcePath, ClassLoader classLoader, String expectedHash) Verifies the integrity of a resource against an expected hash.
-
Method Details
-
computeJarHash
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
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
Compares two hash strings for equality.- Parameters:
hash1- First hashhash2- Second hash- Returns:
- true if both hashes are non-null and equal (case-insensitive), false otherwise
-
verifyJarIntegrity
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 verifyexpectedHash- 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 resourceclassLoader- The ClassLoader to useexpectedHash- The expected SHA-256 hash (hex-encoded)- Returns:
- true if the hash matches, false otherwise
-
performComprehensiveIntegrityCheck
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 verifyexpectedHash- 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
-