verifyFileIntegrity static method

Future<bool> verifyFileIntegrity(
  1. String filePath,
  2. String expectedHash,
  3. HashAlgorithm algorithm
)

验证文件完整性

Implementation

static Future<bool> verifyFileIntegrity(
    String filePath, String expectedHash, HashAlgorithm algorithm) async {
  try {
    String actualHash;
    switch (algorithm) {
      case HashAlgorithm.md5:
        actualHash = await calculateMD5(filePath);
        break;
      case HashAlgorithm.sha1:
        actualHash = await calculateSHA1(filePath);
        break;
      case HashAlgorithm.sha256:
        actualHash = await calculateSHA256(filePath);
        break;
    }

    return actualHash.toLowerCase() == expectedHash.toLowerCase();
  } catch (e) {
    return false;
  }
}