-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
156 lines (133 loc) · 3.19 KB
/
helpers.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
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/sha3"
"log"
"strings"
"unicode/utf8"
)
func printable(s string) string {
s = strings.ReplaceAll(s, "\n", "\\n")
s = strings.ReplaceAll(s, "\r", "\\r")
s = strings.ReplaceAll(s, "\t", "\\t")
return s
}
func reverse(s string) string {
size := len(s)
buf := make([]byte, size)
for start := 0; start < size; {
r, n := utf8.DecodeRuneInString(s[start:])
start += n
utf8.EncodeRune(buf[size-start:], r)
}
return string(buf)
}
func uniqueSlice(arr []string) []string {
mvalues := map[string]string{}
for _, s := range arr {
mvalues[s] = s
}
var filtered []string
for _, s := range mvalues {
filtered = append(filtered, s)
}
return filtered
}
// func hashMD4(s string) string {
// return fmt.Sprintf("%x", md4.Sum([]byte(s)))
// }
func hashMD5(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}
func hashSHA1(s string) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(s)))
}
func hashSHA256(s string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(s)))
}
func hashSHA512(s string) string {
return fmt.Sprintf("%x", sha512.Sum512([]byte(s)))
}
// SHA-224
func hashSHA224(s string) string {
return fmt.Sprintf("%x", sha256.Sum224([]byte(s)))
}
// SHA-384
func hashSHA384(s string) string {
return fmt.Sprintf("%x", sha512.Sum384([]byte(s)))
}
// SHA-512/224
func hashSHA512_224(s string) string {
return fmt.Sprintf("%x", sha512.Sum512_224([]byte(s)))
}
// SHA-512/256
func hashSHA512_256(s string) string {
return fmt.Sprintf("%x", sha512.Sum512_256([]byte(s)))
}
// SHA3-256
func hashSHA3_256(s string) string {
hash := sha3.New256()
hash.Write([]byte(s))
return fmt.Sprintf("%x", hash.Sum(nil))
}
// SHA3-512
func hashSHA3_512(s string) string {
hash := sha3.New512()
hash.Write([]byte(s))
return fmt.Sprintf("%x", hash.Sum(nil))
}
// RIPEMD-160
func hashRIPEMD160(s string) string {
hash := ripemd160.New()
hash.Write([]byte(s))
return fmt.Sprintf("%x", hash.Sum(nil))
}
func permutations(input []string) [][]string {
if len(input) > 10 {
log.Fatal("Too many permutations")
}
var result [][]string
heapPermutation(input, len(input), &result)
return result
}
func heapPermutation(input []string, size int, result *[][]string) {
// If size is 1, store the obtained permutation
if size == 1 {
tmp := make([]string, len(input))
copy(tmp, input)
*result = append(*result, tmp)
}
for i := 0; i < size; i++ {
heapPermutation(input, size-1, result)
// if size is odd, swap the first and last element
// if size is even, swap the i-th and last element
if size%2 == 1 {
input[0], input[size-1] = input[size-1], input[0]
} else {
input[i], input[size-1] = input[size-1], input[i]
}
}
}
func combinations(arr []string) [][]string {
var result [][]string
n := len(arr)
// Total combinations would be 2^n
total := 1 << n
// Iterate from 1 to 2^n
for i := 1; i < total; i++ {
var subset []string
for j := 0; j < n; j++ {
// If j-th bit in i is set, add arr[j] to subset
if (i & (1 << j)) > 0 {
subset = append(subset, arr[j])
}
}
result = append(result, subset)
}
return result
}