74

I'm looking for a way to compute CRC checksums cross platform.

cksum works on Linux, AIX, HP-UX Itanium, Solaris, but is there an equivalent command available in Windows too?

%cksum run.sh
1491301976 652 run.sh

Note: I'm not interested in third party tools.

Andreas
  • 5,393
  • 9
  • 44
  • 53
AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • 3
    On Windows, you can install [Cygwin](http://www.cygwin.com/) or [GNU CoreUtils](http://gnuwin32.sourceforge.net/packages/coreutils.htm) to get `cksum`. – j.w.r Sep 05 '12 at 07:28
  • Windows Subsystem for Linux (WSL) works: `wsl cksum run.sh` – Andreas Mar 19 '20 at 14:49

8 Answers8

149

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://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/cc732443(v=ws.11)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Cassian
  • 3,648
  • 1
  • 29
  • 40
  • It will be a customer system and they might not have a third party tool installed, so I need to go with something which comes within windows. At present I am using java to get this done. Thanks for your answer. – AabinGunz Jul 02 '15 at 05:56
  • 9
    CertUtil comes within windows – Cassian Jul 02 '15 at 14:05
  • side note: cksum in unix, uses CRC32 compliant with POSIX 1003.2 – Cassian Jul 10 '15 at 15:25
  • This is the best built-in answer though the output is incredibly annoying as it has other non-checksum lines printed to stdout before and after the checksum as well (on Windows 10, at least). – James M. Greene Dec 09 '16 at 14:29
9

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

RyanB
  • 107
  • 1
  • 4
  • 3
    N.B. does not support CRC checks, just MD5 and SHA-1. – user667489 Dec 13 '12 at 15:23
  • 1
    IBM has a [list of MD5 checksum apps](http://www-01.ibm.com/support/docview.wss?uid=swg21496703) for different platforms – Larry K Jul 16 '14 at 05:14
  • There is a kind of manual with examples of command line to use the utility at https://en.wikibooks.org/wiki/File_Checksum_Integrity_Verifier_(FCIV)_Examples#Making_Hashes – manuelvigarcia Apr 20 '17 at 07:24
5

To avoid annoying non-checksum lines:

 CertUtil -v -hashfile "your_file" SHA1 | FIND /V "CertUtil"

This will display only line(s) NOT containing CertUtil

phuclv
  • 37,963
  • 15
  • 156
  • 475
HIlitec
  • 59
  • 1
  • 2
3

Open Windows PowerShell, and use the below command:

Get-FileHash C:\Users\Deepak\Downloads\ubuntu-20.10-desktop-amd64.iso
Deepak
  • 69
  • 4
1

Here is a C# implementation of the *nix cksum command line utility for windows https://cksum.codeplex.com/

sst
  • 1,443
  • 1
  • 12
  • 15
  • 1
    Please don't just post a link to the answer, try to explain it yourself. – tux3 Feb 12 '15 at 22:20
  • 4
    @tux3 There is a short explanation that describes the purpose of the link, and the answer is relevant to the question . But since I'm new here and that was my first post in stackoverflow, I may have missed something. So I would be grateful if you describe more clearly to me that what else should be explained, so that I can improve my answers in the future and be more helpful to the community – sst Feb 12 '15 at 23:10
  • "Try to explain it yourself" in a post linking to an *application*. People don't always use common sense... – Glenn Maynard Feb 11 '21 at 04:54
1

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

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 then shows the following

Algorithm : SHA256
Hash      : 123456789ABCDEFGH1234567890...
Path      :  C:\Users\Donald\Downloads\File-to-be-checked-by-sha256.exe
phuclv
  • 37,963
  • 15
  • 156
  • 475
stacki
  • 31
  • 3
1

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

More fustly implementation, with C# code. Works with powershell2.0

$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
phuclv
  • 37,963
  • 15
  • 156
  • 475
azk
  • 11
  • 2
0

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.

Chris
  • 739
  • 2
  • 8
  • 23