-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsecure.go
60 lines (50 loc) · 1.06 KB
/
secure.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
package goclitools
import (
"bytes"
"os"
"strings"
)
func SecureString(input string, secrets []string) string {
if len(secrets) == 0 {
return input
}
r := make([]string, 2*len(secrets))
for i, e := range secrets {
r[i*2] = e
r[i*2+1] = "*******"
}
return strings.NewReplacer(r...).Replace(input)
}
func SecureByteArray(input []byte, secrets []string) []byte {
if len(secrets) == 0 {
return input
}
data := input
for _, e := range secrets {
data = bytes.Replace(data, []byte(e), []byte("*******"), -1)
}
return data
}
func SecureStd(out *os.File, secrets []string) *os.File {
if len(secrets) == 0 {
return out
}
readFile, writeFile, err := os.Pipe()
if err != nil {
return out
}
go func() {
defer readFile.Close()
// MEMO: secrets may be split to multiple buffers and not detected
// possible solution https://golang.org/pkg/bufio/#example_Scanner_lines
var data [250]byte
for {
n, err := readFile.Read(data[:])
if err != nil {
break
}
out.Write(SecureByteArray(data[:n], secrets))
}
}()
return writeFile
}