-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathdetect.go
More file actions
59 lines (46 loc) · 1.25 KB
/
Copy pathdetect.go
File metadata and controls
59 lines (46 loc) · 1.25 KB
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
package keys
import (
"strings"
"unicode/utf8"
)
// DataType is the type of key data.
type DataType = string
const (
// UnknownType is unknown
UnknownType DataType = ""
// IDType is string identifier (keys.ID)
IDType DataType = "id"
// SaltpackArmoredType is armored saltpack encoding.
SaltpackArmoredType DataType = "saltpack-armored"
// SaltpackType is binary saltpack encoding.
// SaltpackType DataType = "saltpack"
// SSHPublicType is ssh public key "ssh-ed25519 AAAAC3Nz..."
SSHPublicType DataType = "ssh-public"
// SSHType is ssh private key "-----BEGIN OPENSSH PRIVATE..."
SSHType DataType = "ssh"
)
// DetectDataType tries to find out what data type the bytes are.
func DetectDataType(b []byte) DataType {
s := ""
if utf8.Valid(b) {
s = strings.TrimSpace(string(b))
}
if s != "" {
if _, err := ParseID(s); err == nil {
return IDType
} else if len(s) < 100 && (strings.HasPrefix(s, "kex1") || strings.HasPrefix(s, "kbx1")) {
return IDType
}
if strings.Contains(s, "BEGIN ") && strings.Contains(s, " MESSAGE") {
return SaltpackArmoredType
}
if strings.HasPrefix(s, "-----BEGIN ") {
return SSHType
}
if strings.HasPrefix(s, "ssh-") {
return SSHPublicType
}
}
// TODO: SaltpackType
return UnknownType
}