/
hashes.go
43 lines (35 loc) · 890 Bytes
/
hashes.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
//
// Implements diverse hash functions to be used when hashing the function
//
// @author: Rafael Campos Nunes <rafaelnunes@engineer.com>
//
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha512"
"encoding/hex"
"hash"
)
// SHA1 returns an hex encoded 160 bit hash of whatever data is passed
func SHA1(data string) []byte {
hasher := sha1.New()
return hashit(hasher, data)
}
// SHA512 returns an hex encoded 512 bit hash of whatever data is passed
func SHA512(data string) []byte {
hasher := sha512.New()
return hashit(hasher, data)
}
// MD5 returns an hex encoded 512 bit hash of whatever data is passed
func MD5(data string) []byte {
hasher := md5.New()
return hashit(hasher, data)
}
func hashit(h hash.Hash, data string) []byte {
h.Write([]byte(data))
result := h.Sum(nil)
b := make([]byte, hex.EncodedLen(len(result)))
hex.Encode(b, result)
return b
}