-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecksum.go
44 lines (39 loc) · 918 Bytes
/
checksum.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
package main
import (
"fmt"
"io"
"hash"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
)
// Stolen from https://github.com/gosexy/checksum/blob/master/checksum.go
func hashLookup(method string) hash.Hash {
var h hash.Hash
switch method {
case "MD5":
h = md5.New()
case "SHA1":
h = sha1.New()
case "SHA224":
h = sha256.New224()
case "SHA256":
h = sha256.New()
case "SHA384":
h = sha512.New384()
case "SHA512":
h = sha512.New()
default:
// NOTE return err, nil ? panic ? ? warning and default to 256 ?
panic("Unknown hashing method.")
}
return h
}
// Checksum uses various methods to compute given content hash
// TODO More methods https://github.com/gosexy/checksum/blob/master/checksum.go
func Checksum(content []byte, method string) string {
hash := hashLookup(method)
io.WriteString(hash, string(content))
return fmt.Sprintf("%x", hash.Sum(nil))
}