简体   繁体   中英

Windows equivalent of linux cksum command

I am looking for a way to compute crc checksum cross platform.

cksum works on Linux, AIX, HP-UX Itanium, Solaris, is there a equivalent command of linux cksum in windows too?

%cksum run.sh
1491301976 652 run.sh

Note : no third party tool

In Windows (command prompt) you can use CertUtil, here is the syntax:

CertUtil [Options] -hashfile InFile [HashAlgorithm]

for syntax explanation type in cmd:

CertUtil -hashfile -?

example:

CertUtil -hashfile C:\myFile.txt MD5

default is SHA1 it supports: MD2, MD4, MD5, SHA1, SHA256, SHA384, SHA512. Unfortunately no CRC32 as Unix shell does.

Here is a link if you want to find out more https://technet.microsoft.com/en-us/library/cc732443.aspx#BKMK_menu

It looks as if there is an unsupported tool for checksums from MS. It's light on features but appears to do what you're asking for. It was published in August of 2012. It's called "Microsoft File Checksum Integrity Verifier".

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

To avoid annoying non-checksum lines : CertUtil -v -hashfile "your_file" SHA1 | FIND /V "CertUtil" This will display only line(s) NOT contaning CertUtil

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

Get-FileHash C:\Users\Deepak\Downloads\ubuntu-20.10-desktop-amd64.iso

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

In the year 2019, Microsoft offers the following solution for Windows 10. This solution works for SHA256 checksum.

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

Press the Windows key. Type PowerShell. Select Windows Powershell. Press Enter key. Paste the command

Get-FileHash C:\\Users\\Donald\\Downloads\\File-to-be-checked-by-sha256.exe | Format-List

Replace File-to-be-checked-by-sha256.exe by the name of your file to be checked.

Replace the path to your path where the file is. Press Enter key. Powershell shows then the following

Algorithm : SHA256 Hash : 123456789ABCDEFGH1234567890... Path : C:\\Users\\Donald\\Downloads\\File-to-be-checked-by-sha256.exe

In combination of answers of @Cassian and @Hllitec and from https://stackoverflow.com/a/42706309/1001717 here my solution, where I put (only!) the checksum value into a variable for further processing:

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

To test the output you can add a piped echo command with the var:

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%

A bit off-topic, but FYI: I used this before uploading my NuGet package to Artifactory. BTW. as alternative you can use JFrog CLI, where checksum is calculated automatically.

Algorithm the cksum command in linux https://www.opennet.ru/man.shtml?topic=cksum&category=1&russian=5

Powershell implementation:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM