c# - Equivalent to Unix cksum in Windows -
c# - Equivalent to Unix cksum in Windows -
i download file , checksum (generated cksum unix command).
so, want, in c# app test if checksum in adequation app downloaded.
i checked @ unix man page of chsum:
cksum command calculates , prints standard output checksum each named file, number of octets in file , filename. cksum uses portable algorithm based on 32-bit cyclic redundancy check. algorithm finds broader spectrum of errors 16-bit algorithms used sum (see sum(1)). crc sum of next expressions, x each byte of file. x^32 + x^26 + x^23 +x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + x^0 results of calculation truncated 32-bit value. number of bytes in file printed.
so wrote simple programme sum :
byte[] arr = file.readallbytes(@"myapp").toarray(); int cksum = 0; foreach (byte x in arr) { cksum += (x ^ 32 + x ^ 26 + x ^ 23 + x ^ 22 + x ^ 16 + x ^ 12 + x ^ 11 + x ^ 10 + x ^ 8 + x ^ 7 + x ^ 5 + x ^ 4 + x ^ 2 + x ^ 1 + x ^ 0); }
but checksums aren't same, how can prepare this?
thanks
edit1) modified algorithm is:
uint cksum = 0; foreach (byte b in arr) { var x = (uint)b; cksum += (intpow(x, 32) + intpow(x, 26) + intpow(x, 23) + intpow(x, 22) + intpow(x, 16) + intpow(x, 12) + intpow(x, 11) + intpow(x, 10) + intpow(x, 8) + intpow(x, 7) + intpow(x, 5) + intpow(x, 4) + intpow(x, 2) + intpow(x, 1) + intpow(x, 0)); }
2) used class crc32 : hashalgorithm
given unix file crc32 : 2774111254
1) gives me : 4243613712 2) gives me : 3143134679 (with seed of 0)what i'm doing wrong !?
in c# ^
symbol exclusive-or operator. want function math.pow.
this gives piower of 2 floating point numbers, alteratives suggested @ how do *integer* exponentiation in c#?
so, code like:
cksum += math.pow(x,32) + math.pow(x,26)
be aware of lastly statement:
the results of calculation truncated 32-bit value. number of bytes in file printed.
is signed (int
) or unsigned (uint
)
you of course of study utilize following: http://www.codeproject.com/articles/35134/how-to-calculate-crc-in-c
c# checksum crc32
Comments
Post a Comment