<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=BSD_checksum</id>
	<title>BSD checksum - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=BSD_checksum"/>
	<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=BSD_checksum&amp;action=history"/>
	<updated>2026-05-15T15:21:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.0</generator>
	<entry>
		<id>http://en.zaoniao.it/index.php?title=BSD_checksum&amp;diff=3049&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;The '''BSD checksum algorithm''' is a commonly used, legacy checksum algorithm. It has been implemented in BSD and is also available thr...&quot;</title>
		<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=BSD_checksum&amp;diff=3049&amp;oldid=prev"/>
		<updated>2019-04-09T05:27:39Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;The &amp;#039;&amp;#039;&amp;#039;BSD checksum algorithm&amp;#039;&amp;#039;&amp;#039; is a commonly used, legacy &lt;a href=&quot;/Checksum&quot; title=&quot;Checksum&quot;&gt;checksum&lt;/a&gt; algorithm. It has been implemented in &lt;a href=&quot;/index.php?title=Berkeley_Software_Distribution&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Berkeley Software Distribution (page does not exist)&quot;&gt;BSD&lt;/a&gt; and is also available thr...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The '''BSD checksum algorithm''' is a commonly used, legacy [[checksum]] algorithm. It has been implemented in [[Berkeley Software Distribution|BSD]] and is also available through the [[sum (Unix)|GNU sum]] command line utility.&lt;br /&gt;
&lt;br /&gt;
==Computation of the BSD checksum==&lt;br /&gt;
Below is the relevant part of the [[GNU]] sum source code ([[GPL]] licensed). It computes a 16-bit checksum by adding up all bytes (8-bit words) of the input data stream. In order to avoid many of the weaknesses of simply adding the data, the checksum accumulator is circular rotated to the right by one bit at each step before the new char is added.&lt;br /&gt;
&amp;amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;amp;gt;&lt;br /&gt;
int bsdChecksumFromFile(FILE *fp) /* The file handle for input data */&lt;br /&gt;
{&lt;br /&gt;
 int ch; /* Each character read. */&lt;br /&gt;
 int checksum = 0; /* The checksum mod 2^16. */&lt;br /&gt;
&lt;br /&gt;
 while ((ch = getc(fp)) != EOF) {&lt;br /&gt;
 checksum = (checksum &amp;amp;gt;&amp;amp;gt; 1) + ((checksum &amp;amp; 1) &amp;amp;lt;&amp;amp;lt; 15);&lt;br /&gt;
 checksum += ch;&lt;br /&gt;
 checksum &amp;amp;= 0xffff; /* Keep it within bounds. */&lt;br /&gt;
 }&lt;br /&gt;
 return checksum;&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/syntaxhighlight&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is a sample java code that calculates an 8-bit checksum. It adds each byte from the input byte array after a circular rotation of the checksum.&lt;br /&gt;
&amp;amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;amp;gt;&lt;br /&gt;
byte checksum(byte[] input) {&lt;br /&gt;
 byte checksum = 0;&lt;br /&gt;
 for (byte cur_byte: input) {&lt;br /&gt;
 checksum = (byte) (((checksum &amp;amp; 0xFF) &amp;amp;gt;&amp;amp;gt;&amp;amp;gt; 1) + ((checksum &amp;amp; 0x1) &amp;amp;lt;&amp;amp;lt; 7)); // Rotate the accumulator&lt;br /&gt;
	checksum = (byte) ((checksum + cur_byte) &amp;amp; 0xFF); // Add the next chunk&lt;br /&gt;
 }&lt;br /&gt;
 return checksum;&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/syntaxhighlight&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description of the algorithm==&lt;br /&gt;
As mentioned above, this algorithm computes a checksum by segmenting the data and adding it to an accumulator that is circular right shifted between each summation. To keep the accumulator within return value bounds, bit-masking with 1's is done.&lt;br /&gt;
&lt;br /&gt;
'''Example:''' Calculating a 4-bit checksum using 4-bit sized segments ([[Endianness|big-endian]])&lt;br /&gt;
&amp;amp;lt;br /&amp;amp;gt;&lt;br /&gt;
 Input: 101110001110 -&amp;amp;gt; three segments: 1011, 1000, 1110.&lt;br /&gt;
'''''Iteration 1:'''''&lt;br /&gt;
 segment: 1011 checksum: 0000 bitmask: 1111&lt;br /&gt;
a) Apply circular shift to the checksum:&lt;br /&gt;
 0000 -&amp;amp;gt; 0000&lt;br /&gt;
b) Add checksum and segment together, apply bitmask onto the obtained result:&lt;br /&gt;
 0000 + 1011 = 1011 -&amp;amp;gt; 1011 &amp;amp; 1111 = 1011&lt;br /&gt;
'''''Iteration 2:''''' &lt;br /&gt;
 segment: 1000 checksum: 1011 bitmask: 1111&lt;br /&gt;
a) Apply circular shift to the checksum:&lt;br /&gt;
 1011 -&amp;amp;gt; 1101&lt;br /&gt;
b) Add checksum and segment together, apply bitmask onto the obtained result:&lt;br /&gt;
 1101 + 1000 = 10101 -&amp;amp;gt; 10101 &amp;amp; 1111 = 0101&lt;br /&gt;
'''''Iteration 3:'''''&lt;br /&gt;
 segment: 1110 checksum: 0101 bitmask: 1111&lt;br /&gt;
a) Apply circular shift to the checksum:&lt;br /&gt;
 0101 -&amp;amp;gt; 1010&lt;br /&gt;
b) Add checksum and segment together, apply bitmask onto the obtained result:&lt;br /&gt;
 1010 + 1110 = 11000 -&amp;amp;gt; 11000 &amp;amp; 1111 = 1000&lt;br /&gt;
'''''Final checksum:''''' 1000&lt;br /&gt;
&lt;br /&gt;
==Sources==&lt;br /&gt;
* [http://svnweb.freebsd.org/base/stable/9/usr.bin/cksum/sum1.c?revision=225736&amp;amp;view=markup official FreeBSD sum source code]&lt;br /&gt;
* [https://www.gnu.org/software/coreutils/manual/html_node/sum-invocation.html official GNU sum manual page]&lt;br /&gt;
* [https://www.gnu.org/software/coreutils/ coreutils download page] --- find and unpack the newest version of the coreutils package, read src/sum.c&lt;br /&gt;
&lt;br /&gt;
==Source==&lt;br /&gt;
&lt;br /&gt;
[http://wikipedia.org/ http://wikipedia.org/]&lt;br /&gt;
==See Also on BitcoinWiki==&lt;br /&gt;
* [[Proof-of-Research Genesis Block]]&lt;br /&gt;
* [[BIP 0109]]&lt;br /&gt;
* [[BIP 0142]]&lt;br /&gt;
* [[Shaping codes]]&lt;br /&gt;
* [[BIP 0140]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
		
	</entry>
</feed>