summaryrefslogtreecommitdiff
path: root/devdocs/go/hash%2Fcrc32%2Findex.html
blob: 65aed1328804e56eda538fe241df9880276248d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<h1> Package crc32  </h1>     <ul id="short-nav">
<li><code>import "hash/crc32"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32, checksum. See <a href="https://en.wikipedia.org/wiki/Cyclic_redundancy_check">https://en.wikipedia.org/wiki/Cyclic_redundancy_check</a> for information. </p>
<p>Polynomials are represented in LSB-first form also known as reversed representation. </p>
<p>See <a href="https://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Reversed_representations_and_reciprocal_polynomials">https://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Reversed_representations_and_reciprocal_polynomials</a> for information. </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#pkg-constants">Constants</a></li>
<li><a href="#pkg-variables">Variables</a></li>
<li><a href="#Checksum">func Checksum(data []byte, tab *Table) uint32</a></li>
<li><a href="#ChecksumIEEE">func ChecksumIEEE(data []byte) uint32</a></li>
<li><a href="#New">func New(tab *Table) hash.Hash32</a></li>
<li><a href="#NewIEEE">func NewIEEE() hash.Hash32</a></li>
<li><a href="#Update">func Update(crc uint32, tab *Table, p []byte) uint32</a></li>
<li><a href="#Table">type Table</a></li>
<li> <a href="#MakeTable">func MakeTable(poly uint32) *Table</a>
</li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_MakeTable">MakeTable</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>crc32.go</span> <span>crc32_amd64.go</span> <span>crc32_generic.go</span>  </p>   <h2 id="pkg-constants">Constants</h2> <p>Predefined polynomials. </p>
<pre data-language="go">const (
    // IEEE is by far and away the most common CRC-32 polynomial.
    // Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ...
    IEEE = 0xedb88320

    // Castagnoli's polynomial, used in iSCSI.
    // Has better error detection characteristics than IEEE.
    // https://dx.doi.org/10.1109/26.231911
    Castagnoli = 0x82f63b78

    // Koopman's polynomial.
    // Also has better error detection characteristics than IEEE.
    // https://dx.doi.org/10.1109/DSN.2002.1028931
    Koopman = 0xeb31d82e
)</pre> <p>The size of a CRC-32 checksum in bytes. </p>
<pre data-language="go">const Size = 4</pre> <h2 id="pkg-variables">Variables</h2> <p>IEEETable is the table for the <a href="#IEEE">IEEE</a> polynomial. </p>
<pre data-language="go">var IEEETable = simpleMakeTable(IEEE)</pre> <h2 id="Checksum">func <span>Checksum</span>  </h2> <pre data-language="go">func Checksum(data []byte, tab *Table) uint32</pre> <p>Checksum returns the CRC-32 checksum of data using the polynomial represented by the <a href="#Table">Table</a>. </p>
<h2 id="ChecksumIEEE">func <span>ChecksumIEEE</span>  </h2> <pre data-language="go">func ChecksumIEEE(data []byte) uint32</pre> <p>ChecksumIEEE returns the CRC-32 checksum of data using the <a href="#IEEE">IEEE</a> polynomial. </p>
<h2 id="New">func <span>New</span>  </h2> <pre data-language="go">func New(tab *Table) hash.Hash32</pre> <p>New creates a new <span>hash.Hash32</span> computing the CRC-32 checksum using the polynomial represented by the <a href="#Table">Table</a>. Its Sum method will lay the value out in big-endian byte order. The returned Hash32 also implements <span>encoding.BinaryMarshaler</span> and <span>encoding.BinaryUnmarshaler</span> to marshal and unmarshal the internal state of the hash. </p>
<h2 id="NewIEEE">func <span>NewIEEE</span>  </h2> <pre data-language="go">func NewIEEE() hash.Hash32</pre> <p>NewIEEE creates a new <span>hash.Hash32</span> computing the CRC-32 checksum using the <a href="#IEEE">IEEE</a> polynomial. Its Sum method will lay the value out in big-endian byte order. The returned Hash32 also implements <span>encoding.BinaryMarshaler</span> and <span>encoding.BinaryUnmarshaler</span> to marshal and unmarshal the internal state of the hash. </p>
<h2 id="Update">func <span>Update</span>  </h2> <pre data-language="go">func Update(crc uint32, tab *Table, p []byte) uint32</pre> <p>Update returns the result of adding the bytes in p to the crc. </p>
<h2 id="Table">type <span>Table</span>  </h2> <p>Table is a 256-word table representing the polynomial for efficient processing. </p>
<pre data-language="go">type Table [256]uint32</pre> <h3 id="MakeTable">func <span>MakeTable</span>  </h3> <pre data-language="go">func MakeTable(poly uint32) *Table</pre> <p>MakeTable returns a <a href="#Table">Table</a> constructed from the specified polynomial. The contents of this <a href="#Table">Table</a> must not be modified. </p>   <h4 id="example_MakeTable"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// In this package, the CRC polynomial is represented in reversed notation,
// or LSB-first representation.
//
// LSB-first representation is a hexadecimal number with n bits, in which the
// most significant bit represents the coefficient of x⁰ and the least significant
// bit represents the coefficient of xⁿ⁻¹ (the coefficient for xⁿ is implicit).
//
// For example, CRC32-Q, as defined by the following polynomial,
//	x³²+ x³¹+ x²⁴+ x²²+ x¹⁶+ x¹⁴+ x⁸+ x⁷+ x⁵+ x³+ x¹+ x⁰
// has the reversed notation 0b11010101100000101000001010000001, so the value
// that should be passed to MakeTable is 0xD5828281.
crc32q := crc32.MakeTable(0xD5828281)
fmt.Printf("%08x\n", crc32.Checksum([]byte("Hello world"), crc32q))
</pre> <p>Output:</p> <pre class="output" data-language="go">2964d064
</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/hash/crc32/" class="_attribution-link">http://golang.org/pkg/hash/crc32/</a>
  </p>
</div>