69

我正在寻找一种跨平台计算 crc 校验和的方法。

cksum适用于 Linux、AIX、HP-UX Itanium、Solaris,cksum在 windows 中是否也有与 linux 等效的命令?

%cksum run.sh
1491301976 652 run.sh

注意:没有第三方工具

4

8 回答 8

142

在 Windows(命令提示符)中,您可以使用 CertUtil,语法如下:

CertUtil [Options] -hashfile InFile [HashAlgorithm]

cmd中的语法解释类型:

CertUtil -hashfile -?

例子:

CertUtil -hashfile C:\myFile.txt MD5

默认为 SHA1 它支持:MD2、MD4、MD5、SHA1、SHA256、SHA384、SHA512。不幸的是,没有像 Unix shell 那样的 CRC32。

如果您想了解更多信息,请点击此处的链接https://technet.microsoft.com/en-us/library/cc732443.aspx#BKMK_menu

于 2015-07-01T09:17:38.037 回答
9

看起来好像有一个不受支持的 MS 校验和工具。它的功能很简单,但似乎可以满足您的要求。它于 2012 年 8 月发布。它被称为“Microsoft File Checksum Integrity Verifier”。

http://www.microsoft.com/en-us/download/details.aspx?id=11533

于 2012-12-10T18:31:18.243 回答
6

为了避免烦人的非校验和行: CertUtil -v -hashfile "your_file" SHA1 | FIND /V "CertUtil" 这将只显示包含 CertUtil的行

于 2018-02-19T01:08:53.847 回答
2

打开 Windows PowerShell,并使用以下命令:

Get-FileHash C:\Users\Deepak\Downloads\ubuntu-20.10-desktop-amd64.iso
于 2020-12-10T13:08:05.080 回答
1

这是用于 Windows https://cksum.codeplex.com/的 *nix cksum 命令行实用程序的 C# 实现

于 2015-02-12T22:16:00.793 回答
1

在 2019 年,Microsoft 为 Windows 10 提供了以下解决方案。此解决方案适用于 SHA256 校验和。

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash?view=powershell-6

按 Windows 键。键入 PowerShell。选择 Windows Powershell。按回车键。粘贴命令

Get-FileHash C:\Users\Donald\Downloads\File-to-be-checked-by-sha256.exe | 格式列表

将 File-to-be-checked-by-sha256.exe 替换为要检查的文件的名称。

将路径替换为文件所在的路径。按回车键。Powershell然后显示以下

算法:SHA256 哈希:123456789ABCDEFGH1234567890... 路径:C:\Users\Donald\Downloads\File-to-be-checked-by-sha256.exe

于 2019-08-04T12:58:18.630 回答
1

算法 linux 中的 cksum 命令 https://www.opennet.ru/man.shtml?topic=cksum&category=1&russian=5

Powershell 实现:

function UNSIGNED($u) {
    return $u -band 0xffffffff
}

function memcrc32($b){

     $crctab = [uint32]"0x00000000", [uint32]"0x04c11db7", [uint32]"0x09823b6e", [uint32]"0x0d4326d9", [uint32]"0x130476dc",
        [uint32]"0x17c56b6b", [uint32]"0x1a864db2", [uint32]"0x1e475005", [uint32]"0x2608edb8", [uint32]"0x22c9f00f",
        [uint32]"0x2f8ad6d6", [uint32]"0x2b4bcb61", [uint32]"0x350c9b64", [uint32]"0x31cd86d3", [uint32]"0x3c8ea00a",
        [uint32]"0x384fbdbd", [uint32]"0x4c11db70", [uint32]"0x48d0c6c7", [uint32]"0x4593e01e", [uint32]"0x4152fda9",
        [uint32]"0x5f15adac", [uint32]"0x5bd4b01b", [uint32]"0x569796c2", [uint32]"0x52568b75", [uint32]"0x6a1936c8",
        [uint32]"0x6ed82b7f", [uint32]"0x639b0da6", [uint32]"0x675a1011", [uint32]"0x791d4014", [uint32]"0x7ddc5da3",
        [uint32]"0x709f7b7a", [uint32]"0x745e66cd", [uint32]"0x9823b6e0", [uint32]"0x9ce2ab57", [uint32]"0x91a18d8e",
        [uint32]"0x95609039", [uint32]"0x8b27c03c", [uint32]"0x8fe6dd8b", [uint32]"0x82a5fb52", [uint32]"0x8664e6e5",
        [uint32]"0xbe2b5b58", [uint32]"0xbaea46ef", [uint32]"0xb7a96036", [uint32]"0xb3687d81", [uint32]"0xad2f2d84",
        [uint32]"0xa9ee3033", [uint32]"0xa4ad16ea", [uint32]"0xa06c0b5d", [uint32]"0xd4326d90", [uint32]"0xd0f37027",
        [uint32]"0xddb056fe", [uint32]"0xd9714b49", [uint32]"0xc7361b4c", [uint32]"0xc3f706fb", [uint32]"0xceb42022",
        [uint32]"0xca753d95", [uint32]"0xf23a8028", [uint32]"0xf6fb9d9f", [uint32]"0xfbb8bb46", [uint32]"0xff79a6f1",
        [uint32]"0xe13ef6f4", [uint32]"0xe5ffeb43", [uint32]"0xe8bccd9a", [uint32]"0xec7dd02d", [uint32]"0x34867077",
        [uint32]"0x30476dc0", [uint32]"0x3d044b19", [uint32]"0x39c556ae", [uint32]"0x278206ab", [uint32]"0x23431b1c",
        [uint32]"0x2e003dc5", [uint32]"0x2ac12072", [uint32]"0x128e9dcf", [uint32]"0x164f8078", [uint32]"0x1b0ca6a1",
        [uint32]"0x1fcdbb16", [uint32]"0x018aeb13", [uint32]"0x054bf6a4", [uint32]"0x0808d07d", [uint32]"0x0cc9cdca",
        [uint32]"0x7897ab07", [uint32]"0x7c56b6b0", [uint32]"0x71159069", [uint32]"0x75d48dde", [uint32]"0x6b93dddb",
        [uint32]"0x6f52c06c", [uint32]"0x6211e6b5", [uint32]"0x66d0fb02", [uint32]"0x5e9f46bf", [uint32]"0x5a5e5b08",
        [uint32]"0x571d7dd1", [uint32]"0x53dc6066", [uint32]"0x4d9b3063", [uint32]"0x495a2dd4", [uint32]"0x44190b0d",
        [uint32]"0x40d816ba", [uint32]"0xaca5c697", [uint32]"0xa864db20", [uint32]"0xa527fdf9", [uint32]"0xa1e6e04e",
        [uint32]"0xbfa1b04b", [uint32]"0xbb60adfc", [uint32]"0xb6238b25", [uint32]"0xb2e29692", [uint32]"0x8aad2b2f",
        [uint32]"0x8e6c3698", [uint32]"0x832f1041", [uint32]"0x87ee0df6", [uint32]"0x99a95df3", [uint32]"0x9d684044",
        [uint32]"0x902b669d", [uint32]"0x94ea7b2a", [uint32]"0xe0b41de7", [uint32]"0xe4750050", [uint32]"0xe9362689",
        [uint32]"0xedf73b3e", [uint32]"0xf3b06b3b", [uint32]"0xf771768c", [uint32]"0xfa325055", [uint32]"0xfef34de2",
        [uint32]"0xc6bcf05f", [uint32]"0xc27dede8", [uint32]"0xcf3ecb31", [uint32]"0xcbffd686", [uint32]"0xd5b88683",
        [uint32]"0xd1799b34", [uint32]"0xdc3abded", [uint32]"0xd8fba05a", [uint32]"0x690ce0ee", [uint32]"0x6dcdfd59",
        [uint32]"0x608edb80", [uint32]"0x644fc637", [uint32]"0x7a089632", [uint32]"0x7ec98b85", [uint32]"0x738aad5c",
        [uint32]"0x774bb0eb", [uint32]"0x4f040d56", [uint32]"0x4bc510e1", [uint32]"0x46863638", [uint32]"0x42472b8f",
        [uint32]"0x5c007b8a", [uint32]"0x58c1663d", [uint32]"0x558240e4", [uint32]"0x51435d53", [uint32]"0x251d3b9e",
        [uint32]"0x21dc2629", [uint32]"0x2c9f00f0", [uint32]"0x285e1d47", [uint32]"0x36194d42", [uint32]"0x32d850f5",
        [uint32]"0x3f9b762c", [uint32]"0x3b5a6b9b", [uint32]"0x0315d626", [uint32]"0x07d4cb91", [uint32]"0x0a97ed48",
        [uint32]"0x0e56f0ff", [uint32]"0x1011a0fa", [uint32]"0x14d0bd4d", [uint32]"0x19939b94", [uint32]"0x1d528623",
        [uint32]"0xf12f560e", [uint32]"0xf5ee4bb9", [uint32]"0xf8ad6d60", [uint32]"0xfc6c70d7", [uint32]"0xe22b20d2",
        [uint32]"0xe6ea3d65", [uint32]"0xeba91bbc", [uint32]"0xef68060b", [uint32]"0xd727bbb6", [uint32]"0xd3e6a601",
        [uint32]"0xdea580d8", [uint32]"0xda649d6f", [uint32]"0xc423cd6a", [uint32]"0xc0e2d0dd", [uint32]"0xcda1f604",
        [uint32]"0xc960ebb3", [uint32]"0xbd3e8d7e", [uint32]"0xb9ff90c9", [uint32]"0xb4bcb610", [uint32]"0xb07daba7",
        [uint32]"0xae3afba2", [uint32]"0xaafbe615", [uint32]"0xa7b8c0cc", [uint32]"0xa379dd7b", [uint32]"0x9b3660c6",
        [uint32]"0x9ff77d71", [uint32]"0x92b45ba8", [uint32]"0x9675461f", [uint32]"0x8832161a", [uint32]"0x8cf30bad",
        [uint32]"0x81b02d74", [uint32]"0x857130c3", [uint32]"0x5d8a9099", [uint32]"0x594b8d2e", [uint32]"0x5408abf7",
        [uint32]"0x50c9b640", [uint32]"0x4e8ee645", [uint32]"0x4a4ffbf2", [uint32]"0x470cdd2b", [uint32]"0x43cdc09c",
        [uint32]"0x7b827d21", [uint32]"0x7f436096", [uint32]"0x7200464f", [uint32]"0x76c15bf8", [uint32]"0x68860bfd",
        [uint32]"0x6c47164a", [uint32]"0x61043093", [uint32]"0x65c52d24", [uint32]"0x119b4be9", [uint32]"0x155a565e",
        [uint32]"0x18197087", [uint32]"0x1cd86d30", [uint32]"0x029f3d35", [uint32]"0x065e2082", [uint32]"0x0b1d065b",
        [uint32]"0x0fdc1bec", [uint32]"0x3793a651", [uint32]"0x3352bbe6", [uint32]"0x3e119d3f", [uint32]"0x3ad08088",
        [uint32]"0x2497d08d", [uint32]"0x2056cd3a", [uint32]"0x2d15ebe3", [uint32]"0x29d4f654", [uint32]"0xc5a92679",
        [uint32]"0xc1683bce", [uint32]"0xcc2b1d17", [uint32]"0xc8ea00a0", [uint32]"0xd6ad50a5", [uint32]"0xd26c4d12",
        [uint32]"0xdf2f6bcb", [uint32]"0xdbee767c", [uint32]"0xe3a1cbc1", [uint32]"0xe760d676", [uint32]"0xea23f0af",
        [uint32]"0xeee2ed18", [uint32]"0xf0a5bd1d", [uint32]"0xf464a0aa", [uint32]"0xf9278673", [uint32]"0xfde69bc4",
        [uint32]"0x89b8fd09", [uint32]"0x8d79e0be", [uint32]"0x803ac667", [uint32]"0x84fbdbd0", [uint32]"0x9abc8bd5",
        [uint32]"0x9e7d9662", [uint32]"0x933eb0bb", [uint32]"0x97ffad0c", [uint32]"0xafb010b1", [uint32]"0xab710d06",
        [uint32]"0xa6322bdf", [uint32]"0xa2f33668", [uint32]"0xbcb4666d", [uint32]"0xb8757bda", [uint32]"0xb5365d03",
        [uint32]"0xb1f740b4"

    $n = $b.length
    $i = $c = $s = 0

    foreach ($c in $b){
      $tabidx = (($s -shr 24) -bxor $c)
      $s = ((UNSIGNED($s -shl 8)) -bxor $crctab[$tabidx])
    }

   while($n -ne 0){
        $c = ($n -band 0xff)
        $n = ($n -shr 8)
        $s = (($s -shl 8) -bxor ($crctab[($s -shr 24) -bxor $c]))
   }
   return (-bnot $s)
}


function cksum($fname) {
     
      $buffer=[System.IO.File]::ReadAllBytes($fname)
      $CRC = memcrc32($buffer)
      
      Write-Host $CRC $buffer.length "$fname"
    }


cksum("PATH_TO_FILE")
于 2021-12-10T08:49:11.187 回答
0

结合@Cassian和@Hllitec的答案以及来自https://stackoverflow.com/a/42706309/1001717的解决方案,我将(仅!)校验和值放入变量中以进行进一步处理:

for /f "delims=" %i in ('certutil -v -hashfile myPackage.nupkg SHA256 ^| find /i /v "sha256" ^| find /i /v "certutil"') do set myVar=%i

要测试输出,您可以使用 var 添加管道 echo 命令:

for /f "delims=" %i in ('certutil -v -hashfile myPackage.nupkg SHA256 ^| find /i /v "sha256" ^| find /i /v "certutil"') do set myVar=%i | echo %myVar%

有点离题,但仅供参考:我在将 NuGet 包上传到 Artifactory 之前使用了它。顺便提一句。作为替代方案,您可以使用 JFrog CLI,其中会自动计算校验和。

于 2019-04-18T11:15:01.753 回答