/
main.go
172 lines (151 loc) · 3.64 KB
/
main.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2016 Lennart Espe. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
/*
Gohash generates file hashes.
Usage:
godoc [flag] file
The flags are:
-a algorithm
Available algorithms are crc32, crc64, adler32, fnv32, fnva32, fnv64, fnva64, sha1, sha256, sha512
--version
Shows the current verion of Gohash
--help
Displays the help page
*/
package main
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"flag"
"fmt"
"hash"
"hash/adler32"
"hash/crc32"
"hash/crc64"
"hash/fnv"
"io/ioutil"
"strings"
"github.com/lnsp/pkginfo"
)
var (
PkgInfo = pkginfo.PackageInfo{
Name: "gohash",
Version: pkginfo.PackageVersion{
Major: 1,
Minor: 1,
Patch: 0,
},
}
)
// Returns the 32 bit hash of the file using the provided algorithm
func getHash32(filename string, hs hash.Hash32) (uint32, error) {
bs, err := ioutil.ReadFile(filename)
if err != nil {
return 0, err
}
hs.Write(bs)
return hs.Sum32(), nil
}
// Returns the 64 bit hash of the file using the provided algorithm
func getHash64(filename string, hs hash.Hash64) (uint64, error) {
bs, err := ioutil.ReadFile(filename)
if err != nil {
return 0, err
}
hs.Write(bs)
return hs.Sum64(), nil
}
func getHashArray(filename string, hs hash.Hash) ([]byte, error) {
bs, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
hs.Write(bs)
return hs.Sum(nil), nil
}
// Maps the name to an algorithm and hashes the file
func hashFile(filename string, algorithm string) (string, error) {
if strings.HasPrefix(algorithm, "sha") {
var result []byte
var err error
switch algorithm {
case "sha1":
result, err = getHashArray(filename, sha1.New())
case "sha256":
result, err = getHashArray(filename, sha256.New())
case "sha512":
result, err = getHashArray(filename, sha512.New())
}
if err != nil {
return "", err
}
return hex.EncodeToString(result), nil
}
if strings.HasSuffix(algorithm, "32") {
var result uint32
var err error
switch algorithm {
case "crc32":
result, err = getHash32(filename, crc32.NewIEEE())
case "adler32":
result, err = getHash32(filename, adler32.New())
case "fnv32":
result, err = getHash32(filename, fnv.New32())
case "fnva32":
result, err = getHash32(filename, fnv.New32a())
default:
return "", errors.New("unknown algorithm")
}
if err != nil {
return "", err
}
return fmt.Sprintf("%x", result), nil
} else if strings.HasSuffix(algorithm, "64") {
var result uint64
var err error
switch algorithm {
case "crc64":
result, err = getHash64(filename, crc64.New(crc64.MakeTable(crc64.ISO)))
case "fnv64":
result, err = getHash64(filename, fnv.New64())
case "fnva64":
result, err = getHash64(filename, fnv.New64a())
default:
return "", errors.New("unknown algorithm")
}
if err != nil {
return "", err
}
return fmt.Sprintf("%x", result), nil
} else {
return "", errors.New("unknown algorithm")
}
}
func main() {
version := flag.Bool("version", false, "display version information")
help := flag.Bool("help", false, "show help information")
algorithm := flag.String("a", "crc32", "set hash algorithm (`crc32`, crc64, adler32, fnv32, fnva32, fnv64, fnva64)")
flag.Parse()
// Show help information if requested
if *help {
fmt.Println("gohash [-a] [--help] [--version] [file]")
flag.PrintDefaults()
return
}
// Show version information if requested
if *version {
fmt.Println(PkgInfo)
return
}
filename := flag.Arg(0)
result, err := hashFile(filename, *algorithm)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(result)
}