-
-
Save globby/9337839 to your computer and use it in GitHub Desktop.
def Fletcher16(string): | |
a = map(ord,string) | |
b = [sum(a[:i])%255 for i in range(len(a)+1)] | |
return (sum(b) << 8) | max(b) | |
def Fletcher32(string): | |
a = map(ord,string) | |
b = [sum(a[:i])%65535 for i in range(len(a)+1)] | |
return (sum(b) << 16) | max(b) | |
def Fletcher64(string): | |
a = map(ord,string) | |
b = [sum(a[:i])%4294967295 for i in range(len(a)+1)] | |
return (sum(b) << 32) | max(b) |
I am using the above mentioned code, i should get 4 characters only. But i am getting "221145" Something like this.
Is this Hex data .
http://www.ozeki.hu/p_1614-crc-16-checksum-generator.html
above link, i am using to get Fletchers CRC.
The above function returns the integer representation, not a string.
You can convert around, e.g., using Python's binascii.hexlify
after conversion to bytes:
f = fletcher32("python")
f_bytes = f.to_bytes(4, byteorder='big')
binascii.hexlify(f_bytes)
see also https://docs.python.org/3/library/binascii.html#binascii.hexlify
Another neat web tool for all sorts of things is the GCHQ's CyberChef.
https://gchq.github.io/CyberChef/#recipe=Fletcher-32_Checksum() prints you the hexadecimal representation as well.
For long inputs, this doesn't seem to work though. I compared the output against CyberChef's for the first paragraph from cipsum.com.
Here is another, less compact one: https://github.com/njaladan/hashpy/blob/master/hashpy/fletcherNbit.py
I think you have to apply modulo before summing.
Python 3.x version: