blob: 668f5d005044b9b6fa4f0c89ac47c60baedbce48 (
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
|
package main
import (
"encoding/binary"
"hash/crc32"
"io/ioutil"
"os"
)
func main() {
// Loop through arguments
for _, f := range os.Args[1:] {
// Read the file to memory and panic if it fails
b, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
}
// Calculate CRC32 with IEEE polynomials
c := crc32.ChecksumIEEE(b)
// Open the same file for appending and panic if it fails
f, err := os.OpenFile(f, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer f.Close()
// Create little-endian represenation of CRC32 sum
le := make([]byte, crc32.Size)
binary.LittleEndian.PutUint32(le, c)
// Append the CRC32 and panic if it fails
if _, err = f.Write(le); err != nil {
panic(err)
}
}
}
|