forked from eth0izzle/shhgit
/
util.go
71 lines (56 loc) · 1.08 KB
/
util.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
package core
import (
"crypto/sha1"
"encoding/hex"
"math"
"os"
"path/filepath"
"strings"
)
func GetTempDir(suffix string) string {
dir := filepath.Join(*session.Options.TempDirectory, suffix)
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, os.ModePerm)
} else {
os.RemoveAll(dir)
}
return dir
}
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func LogIfError(text string, err error) {
if err != nil {
GetSession().Log.Error("%s (%s", text, err.Error())
}
}
func GetHash(s string) string {
h := sha1.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
func Pluralize(count int, singular string, plural string) string {
if count == 1 {
return singular
}
return plural
}
func GetEntropy(data string) (entropy float64) {
if data == "" {
return 0
}
for i := 0; i < 256; i++ {
px := float64(strings.Count(data, string(byte(i)))) / float64(len(data))
if px > 0 {
entropy += -px * math.Log2(px)
}
}
return entropy
}