github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/crc/crc.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Prints crc checksum of a file. 6 // 7 // Synopsis: 8 // crc OPTIONS [FILE] 9 // 10 // Description: 11 // One of the crc types must be specified. If there is no file, stdin is 12 // read. 13 // 14 // Options: 15 // -f: CRC function to use. May be one of the following: 16 // crc32-ieee: CRC-32 IEEE standard (default) 17 // crc32-castognoli: CRC-32 Castognoli standard 18 // crc32-koopman: CRC-32 Koopman standard 19 // crc64-ecma: CRC-64 ECMA standard 20 // crc64-iso: CRC-64 ISO standard 21 package main 22 23 import ( 24 "flag" 25 "fmt" 26 "hash" 27 "hash/crc32" 28 "hash/crc64" 29 "io" 30 "log" 31 "os" 32 ) 33 34 var function = flag.String("f", "crc32-ieee", "CRC function") 35 36 func main() { 37 flag.Parse() 38 39 functions := map[string]hash.Hash{ 40 "crc32-ieee": crc32.New(crc32.MakeTable(crc32.IEEE)), 41 "crc32-castognoli": crc32.New(crc32.MakeTable(crc32.Castagnoli)), 42 "crc32-koopman": crc32.New(crc32.MakeTable(crc32.Koopman)), 43 "crc64-ecma": crc64.New(crc64.MakeTable(crc64.ECMA)), 44 "crc64-iso": crc64.New(crc64.MakeTable(crc64.ISO)), 45 } 46 47 h, ok := functions[*function] 48 if !ok { 49 expected := "" 50 for k := range functions { 51 expected += " " + k 52 } 53 log.Fatalf("invalid function %q, expected one of:%s", *function, expected) 54 } 55 56 var r io.Reader 57 switch flag.NArg() { 58 case 0: 59 r = os.Stdin 60 case 1: 61 f, err := os.Open(flag.Arg(0)) 62 if err != nil { 63 log.Fatal(err) 64 } 65 defer f.Close() 66 r = f 67 default: 68 log.Fatal("expected 0 or 1 positional args") 69 } 70 71 if _, err := io.Copy(h, r); err != nil { 72 log.Fatal(err) 73 } 74 fmt.Printf("%x\n", h.Sum([]byte{})) 75 }