我正在寻找一种方法来计算 crc 校验和跨平台。
在 AIX 的 Linux、 HP-UX、 Itanium、 Solaris 上都可以使用,在 windows 系统中是否也可以使用 Linux 的 cksum命令?
cksum
%cksum run.sh 1491301976 652 run.sh
注意 : 没有第三方工具
它看起来好像有一个不受支持的工具从 MS 校验和。它的功能很少,但似乎做你要求的。它于2012年8月出版。它被称为“ Microsoft 文件校验和完整性验证器”。
Http://www.microsoft.com/en-us/download/details.aspx?id=11533
下面是 Windows https://cksum.codeplex.com/的 * nix cksum 命令行实用程序的 C # 实现
在 Windows (命令提示符)中可以使用 CertUtil,语法如下:
CertUtil [Options] -hashfile InFile [HashAlgorithm]
对于 cmd 中的语法解释类型:
CertUtil -hashfile -?
例如:
CertUtil -hashfile C:\myFile.txt MD5
默认是 SHA1,它支持: MD2,MD4,MD5,SHA1,SHA256,SHA384,SHA512。
这里有一个链接,如果你想找到更多的 https://technet.microsoft.com/en-us/library/cc732443.aspx#BKMK_menu
为了避免恼人的非校验和行: CertUtil-v-hashfile“ your _ file”SHA1 | FIND/V“ CertUtil” 这将只显示包含 CertUtil 的 没有行
结合了@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年,微软为 Windows10提供了以下解决方案。该解决方案适用于 SHA256校验和。
Https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash?view=powershell-6
按下 Windows 键,键入 PowerShell,选择 Windows PowerShell,按下回车键,粘贴命令
Get-FileHash C: 用户 Donald 下载 File-to-be-check-by-sha256.exe | Format-List
替换 待检查的文件由 sha256. exe 被检查的文件的名称。
替换文件所在路径的路径。 按回车键。 然后 Powershell 显示以下内容
算法: SHA256 Hash: 123456789ABCDEFGH1234567890... 路径: C: 用户 Donald 下载 File-to-be-check-by-sha256. exe
打开 Windows PowerShell,使用以下命令:
Get-FileHash C:\Users\Deepak\Downloads\ubuntu-20.10-desktop-amd64.iso
在 linux 中对校验和命令进行算法 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")
$Source = @" using System; public class memcrc { public uint check(byte[] b) { uint[] crctab = { 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072, 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53, 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3, 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4 }; uint i=0, tabidx=0; uint c = 0, s = 0; uint n = (uint) b.Length; for (i = 0; i < n; i++) { c = b[i]; tabidx = (s >> 24) ^ c; s = (s << 8) ^ crctab[tabidx]; } while (n != 0) { c = n & 0xff; n >>= 8; s = (s << 8) ^ crctab[(s >> 24) ^ c]; } return ~s; } } "@ Add-Type -TypeDefinition $Source $memcrc32 = New-Object memcrc function cksum($fname) { $buffer=[System.IO.File]::ReadAllBytes($fname) $CRC = $memcrc32.check($buffer) return $CRC } cksum C:\File.txt
使用 C # 代码更全面地实现。 可以使用 powershell2.0