62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package db
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strings"
|
|
|
|
"git.tcp.direct/tcp.direct/IR5EC/connection"
|
|
"git.tcp.direct/tcp.direct/IR5EC/crypto"
|
|
)
|
|
|
|
var trustRegistry = map[string]bool{}
|
|
var trustRegistryLoaded = false
|
|
|
|
func loadTrustRegistry() {
|
|
if trustRegistryLoaded {
|
|
return
|
|
}
|
|
|
|
reg := crypto.RSADecrypt(connection.SharedPrivateKey, string(file))
|
|
if reg != "" {
|
|
lines := strings.Split(string(reg), "\n")
|
|
for _, key := range lines {
|
|
trustRegistry[key] = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func IsTrustedKey(fingerprint string) bool {
|
|
loadTrustRegistry()
|
|
return trustRegistry[fingerprint]
|
|
}
|
|
|
|
func addTrustedKey(fingerprint string) {
|
|
loadTrustRegistry()
|
|
trustRegistry[fingerprint] = true
|
|
saveTrustRegistry()
|
|
}
|
|
|
|
func removeTrustedKey(fingerprint string) {
|
|
loadTrustRegistry()
|
|
delete(trustRegistry, fingerprint)
|
|
saveTrustRegistry()
|
|
}
|
|
|
|
func saveTrustRegistry() {
|
|
lines := make([]string, len(trustRegistry))
|
|
index := 0
|
|
for key := range trustRegistry {
|
|
lines[index] = key
|
|
index++
|
|
}
|
|
|
|
linesString := strings.Join(lines, "\n")
|
|
|
|
data := crypto.RSAEncrypt(&sharedPrivateKey.PublicKey, linesString)
|
|
|
|
err := ioutil.WriteFile(registryPath(), []byte(data), perms)
|
|
fmt.Println(err, "<ER")
|
|
}
|