Class OpenChecksum

java.lang.Object
cloud.opencode.base.io.checksum.OpenChecksum

public final class OpenChecksum extends Object
Checksum Utility Class 校验和工具类

Utility class for calculating checksums and hash values. Supports CRC32, MD5, SHA-1, SHA-256 and other algorithms.

用于计算校验和和哈希值的工具类。 支持CRC32、MD5、SHA-1、SHA-256等算法。

Features | 主要功能:

  • CRC32 checksum - CRC32校验和
  • MD5 hash - MD5哈希
  • SHA-256 hash - SHA-256哈希
  • Hash verification - 哈希验证

Usage Examples | 使用示例:

// CRC32
long crc = OpenChecksum.crc32(path);

// MD5
String md5 = OpenChecksum.md5(path);

// SHA-256
String sha256 = OpenChecksum.sha256(path);

// Verify
boolean valid = OpenChecksum.verify(path, expectedHash, "SHA-256");

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static Checksum
    calculate(InputStream input, String algorithm)
    Calculates digest and returns Checksum object 计算摘要并返回Checksum对象
    static Checksum
    calculate(Path path, String algorithm)
    Calculates digest and returns Checksum object 计算摘要并返回Checksum对象
    static long
    crc32(byte[] data)
    Calculates CRC32 checksum for byte array 计算字节数组的CRC32校验和
    static long
    Calculates CRC32 checksum for an input stream 计算输入流的CRC32校验和
    static long
    crc32(Path path)
    Calculates CRC32 checksum for a file 计算文件的CRC32校验和
    static String
    digest(byte[] data, String algorithm)
    Calculates digest for byte array 计算字节数组的摘要
    static String
    digest(InputStream input, String algorithm)
    Calculates digest for an input stream 计算输入流的摘要
    static String
    digest(Path path, String algorithm)
    Calculates digest for a file using specified algorithm 使用指定算法计算文件的摘要
    static String
    md5(byte[] data)
    Calculates MD5 hash for byte array 计算字节数组的MD5哈希
    static String
    Calculates MD5 hash for an input stream 计算输入流的MD5哈希
    static String
    md5(Path path)
    Calculates MD5 hash for a file 计算文件的MD5哈希
    static String
    sha1(byte[] data)
    Calculates SHA-1 hash for byte array 计算字节数组的SHA-1哈希
    static String
    Calculates SHA-1 hash for an input stream 计算输入流的SHA-1哈希
    static String
    sha1(Path path)
    Calculates SHA-1 hash for a file 计算文件的SHA-1哈希
    static String
    sha256(byte[] data)
    Calculates SHA-256 hash for byte array 计算字节数组的SHA-256哈希
    static String
    Calculates SHA-256 hash for an input stream 计算输入流的SHA-256哈希
    static String
    sha256(Path path)
    Calculates SHA-256 hash for a file 计算文件的SHA-256哈希
    static String
    sha512(byte[] data)
    Calculates SHA-512 hash for byte array 计算字节数组的SHA-512哈希
    static String
    Calculates SHA-512 hash for an input stream 计算输入流的SHA-512哈希
    static String
    sha512(Path path)
    Calculates SHA-512 hash for a file 计算文件的SHA-512哈希
    static boolean
    verify(byte[] data, String expectedHash, String algorithm)
    Verifies byte array checksum 验证字节数组校验和
    static boolean
    verify(InputStream input, String expectedHash, String algorithm)
    Verifies input stream checksum 验证输入流校验和
    static boolean
    verify(Path path, String expectedHash, String algorithm)
    Verifies file checksum 验证文件校验和

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • crc32

      public static long crc32(Path path)
      Calculates CRC32 checksum for a file 计算文件的CRC32校验和
      Parameters:
      path - the file path | 文件路径
      Returns:
      CRC32 value | CRC32值
    • crc32

      public static long crc32(InputStream input)
      Calculates CRC32 checksum for an input stream 计算输入流的CRC32校验和
      Parameters:
      input - the input stream | 输入流
      Returns:
      CRC32 value | CRC32值
    • crc32

      public static long crc32(byte[] data)
      Calculates CRC32 checksum for byte array 计算字节数组的CRC32校验和
      Parameters:
      data - the data | 数据
      Returns:
      CRC32 value | CRC32值
    • md5

      public static String md5(Path path)
      Calculates MD5 hash for a file 计算文件的MD5哈希
      Parameters:
      path - the file path | 文件路径
      Returns:
      MD5 hex string | MD5十六进制字符串
    • md5

      public static String md5(InputStream input)
      Calculates MD5 hash for an input stream 计算输入流的MD5哈希
      Parameters:
      input - the input stream | 输入流
      Returns:
      MD5 hex string | MD5十六进制字符串
    • md5

      public static String md5(byte[] data)
      Calculates MD5 hash for byte array 计算字节数组的MD5哈希
      Parameters:
      data - the data | 数据
      Returns:
      MD5 hex string | MD5十六进制字符串
    • sha1

      public static String sha1(Path path)
      Calculates SHA-1 hash for a file 计算文件的SHA-1哈希
      Parameters:
      path - the file path | 文件路径
      Returns:
      SHA-1 hex string | SHA-1十六进制字符串
    • sha1

      public static String sha1(InputStream input)
      Calculates SHA-1 hash for an input stream 计算输入流的SHA-1哈希
      Parameters:
      input - the input stream | 输入流
      Returns:
      SHA-1 hex string | SHA-1十六进制字符串
    • sha1

      public static String sha1(byte[] data)
      Calculates SHA-1 hash for byte array 计算字节数组的SHA-1哈希
      Parameters:
      data - the data | 数据
      Returns:
      SHA-1 hex string | SHA-1十六进制字符串
    • sha256

      public static String sha256(Path path)
      Calculates SHA-256 hash for a file 计算文件的SHA-256哈希
      Parameters:
      path - the file path | 文件路径
      Returns:
      SHA-256 hex string | SHA-256十六进制字符串
    • sha256

      public static String sha256(InputStream input)
      Calculates SHA-256 hash for an input stream 计算输入流的SHA-256哈希
      Parameters:
      input - the input stream | 输入流
      Returns:
      SHA-256 hex string | SHA-256十六进制字符串
    • sha256

      public static String sha256(byte[] data)
      Calculates SHA-256 hash for byte array 计算字节数组的SHA-256哈希
      Parameters:
      data - the data | 数据
      Returns:
      SHA-256 hex string | SHA-256十六进制字符串
    • sha512

      public static String sha512(Path path)
      Calculates SHA-512 hash for a file 计算文件的SHA-512哈希
      Parameters:
      path - the file path | 文件路径
      Returns:
      SHA-512 hex string | SHA-512十六进制字符串
    • sha512

      public static String sha512(InputStream input)
      Calculates SHA-512 hash for an input stream 计算输入流的SHA-512哈希
      Parameters:
      input - the input stream | 输入流
      Returns:
      SHA-512 hex string | SHA-512十六进制字符串
    • sha512

      public static String sha512(byte[] data)
      Calculates SHA-512 hash for byte array 计算字节数组的SHA-512哈希
      Parameters:
      data - the data | 数据
      Returns:
      SHA-512 hex string | SHA-512十六进制字符串
    • digest

      public static String digest(Path path, String algorithm)
      Calculates digest for a file using specified algorithm 使用指定算法计算文件的摘要
      Parameters:
      path - the file path | 文件路径
      algorithm - the algorithm (MD5, SHA-1, SHA-256, etc.) | 算法
      Returns:
      hex string | 十六进制字符串
    • digest

      public static String digest(InputStream input, String algorithm)
      Calculates digest for an input stream 计算输入流的摘要
      Parameters:
      input - the input stream | 输入流
      algorithm - the algorithm | 算法
      Returns:
      hex string | 十六进制字符串
    • digest

      public static String digest(byte[] data, String algorithm)
      Calculates digest for byte array 计算字节数组的摘要
      Parameters:
      data - the data | 数据
      algorithm - the algorithm | 算法
      Returns:
      hex string | 十六进制字符串
    • calculate

      public static Checksum calculate(Path path, String algorithm)
      Calculates digest and returns Checksum object 计算摘要并返回Checksum对象
      Parameters:
      path - the file path | 文件路径
      algorithm - the algorithm | 算法
      Returns:
      Checksum object | Checksum对象
    • calculate

      public static Checksum calculate(InputStream input, String algorithm)
      Calculates digest and returns Checksum object 计算摘要并返回Checksum对象
      Parameters:
      input - the input stream | 输入流
      algorithm - the algorithm | 算法
      Returns:
      Checksum object | Checksum对象
    • verify

      public static boolean verify(Path path, String expectedHash, String algorithm)
      Verifies file checksum 验证文件校验和
      Parameters:
      path - the file path | 文件路径
      expectedHash - the expected hash hex string | 期望的哈希十六进制字符串
      algorithm - the algorithm (MD5, SHA-256, etc.) | 算法
      Returns:
      true if match | 如果匹配返回true
    • verify

      public static boolean verify(InputStream input, String expectedHash, String algorithm)
      Verifies input stream checksum 验证输入流校验和
      Parameters:
      input - the input stream | 输入流
      expectedHash - the expected hash hex string | 期望的哈希十六进制字符串
      algorithm - the algorithm | 算法
      Returns:
      true if match | 如果匹配返回true
    • verify

      public static boolean verify(byte[] data, String expectedHash, String algorithm)
      Verifies byte array checksum 验证字节数组校验和
      Parameters:
      data - the data | 数据
      expectedHash - the expected hash hex string | 期望的哈希十六进制字符串
      algorithm - the algorithm | 算法
      Returns:
      true if match | 如果匹配返回true