fckeuspy-go/main.go

189 lines
5.2 KiB
Go

package main
import (
"crypto/rsa"
encrypt "fckeuspy-go/lib"
"html/template"
"log"
"net/http"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
var (
priv *rsa.PrivateKey
pubPEM []byte
certPEM []byte // self-signed cert jen pro sdílení identity (volitelné)
tmpl *template.Template
privPath = "identity_key.pem"
pubPath = "public.pem"
certPath = "identity.crt"
)
type envelope struct {
// Encrypted AES key, Nonce, Ciphertext (GCM)
EK string `json:"ek"` // base64(RSA-OAEP(aesKey))
N string `json:"n"` // base64(nonce 12B)
CT string `json:"ct"` // base64(GCM(ciphertext||tag))
}
func main() {
var (
err error
svc *encrypt.Service
)
storageDir, w := NewUI()
// 2) inicializuj šifrovací službu
svc, err = encrypt.NewService(storageDir)
if err != nil {
log.Fatal(err)
}
// Toast (stavový pruh)
toast := widget.NewLabel("")
toast.Hide()
showToast := func(msg string) {
toast.SetText(msg)
toast.Show()
time.AfterFunc(1200*time.Millisecond, func() { toast.SetText(""); toast.Hide() })
}
// Helpers
copyToClipboard := func(s string) { w.Clipboard().SetContent(s); showToast("Zkopírováno") }
/* setErr := func(e error) {
if e != nil {
dialog.ShowError(e, w)
}
}
*/
outKey := widget.NewMultiLineEntry()
cipherOut := widget.NewMultiLineEntry()
// cipherOut.SetReadOnly(true)
plainOut := widget.NewMultiLineEntry()
// plainOut.SetReadOnly(true)
// toastLbl := widget.NewLabelWithData(toastB)
// Widgets
outKey = widget.NewMultiLineEntry()
outKey.SetPlaceHolder("Veřejný klíč / certifikát…")
msg := widget.NewMultiLineEntry()
msg.SetPlaceHolder("Sem napiš zprávu…")
peer := widget.NewMultiLineEntry()
peer.SetPlaceHolder("-----BEGIN PUBLIC KEY----- … nebo CERTIFICATE …")
cipherOut = widget.NewMultiLineEntry()
cipherOut.SetPlaceHolder(`{"ek":"…","n":"…","ct":"…"}`)
// cipherOut.SetReadOnly(true)
payload := widget.NewMultiLineEntry()
payload.SetPlaceHolder(`{"ek":"…","n":"…","ct":"…"}`)
plainOut = widget.NewMultiLineEntry()
plainOut.SetPlaceHolder("Dešifrovaná zpráva…")
// plainOut.SetReadOnly(true)
// Sekce: Můj veřejný klíč
btnShowPub := widget.NewButton("Zobrazit public.pem", func() { outKey.SetText(svc.PublicPEM()) })
btnShowCrt := widget.NewButton("Zobrazit identity.crt", func() { outKey.SetText(svc.PublicCert()) })
btnCopyOut := widget.NewButton("Copy", func() { copyToClipboard(outKey.Text) })
btnClearOut := widget.NewButton("Clear", func() { outKey.SetText("") })
myKeyCard := container.NewVBox(
widget.NewLabelWithStyle("Můj veřejný klíč", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
container.New(layout.NewGridLayout(4), btnShowPub, btnShowCrt, btnCopyOut, btnClearOut),
outKey,
)
// Sekce: Šifrovat
btnEnc := widget.NewButton("Encrypt", func() {
m := msg.Text
p := peer.Text
if m == "" {
dialog.ShowInformation("Info", "Zpráva nesmí být prázdná.", w)
return
}
if p == "" {
dialog.ShowInformation("Info", "Vlož veřejný klíč (PEM) nebo certifikát (CERT).", w)
return
}
go func() {
res, err := svc.Encrypt(m, p)
if err != nil {
cipherOut.SetText("")
toast.SetText("Chyba: " + err.Error())
toast.Show()
return
}
cipherOut.SetText(res)
showToast("Zašifrováno")
}()
})
btnCopyCipher := widget.NewButton("Copy", func() { copyToClipboard(cipherOut.Text) })
encCard := container.NewVBox(
widget.NewLabelWithStyle("Šifrovat pro cizí klíč", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
widget.NewLabel("Zpráva"), msg,
widget.NewLabel("Veřejný klíč příjemce (PEM nebo CERT)"), peer,
container.New(layout.NewGridLayout(2), btnEnc, btnCopyCipher),
cipherOut,
)
// Sekce: Dešifrovat
btnDec := widget.NewButton("Decrypt", func() {
pl := payload.Text
if pl == "" {
dialog.ShowInformation("Info", "Vlož JSON payload k dešifrování.", w)
return
}
go func() {
res, err := svc.Decrypt(pl)
if err != nil {
plainOut.SetText("")
toast.SetText("Chyba: " + err.Error())
toast.Show()
return
}
plainOut.SetText(res)
showToast("Dešifrováno")
}()
})
btnCopyPlain := widget.NewButton("Copy", func() { copyToClipboard(plainOut.Text) })
btnClearPayload := widget.NewButton("Clear payload", func() { payload.SetText("") })
decCard := container.NewVBox(
widget.NewLabelWithStyle("Dešifrovat", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
widget.NewLabel("Payload (JSON envelope)"), payload,
container.New(layout.NewGridLayout(3), btnDec, btnCopyPlain, btnClearPayload),
plainOut,
)
left := container.NewVBox(myKeyCard, encCard)
right := container.NewVBox(decCard)
grid := container.New(layout.NewGridLayoutWithColumns(2), left, right)
content := container.NewBorder(nil, toast, nil, nil, container.NewVScroll(grid))
w.SetContent(content)
w.ShowAndRun()
}
func RunWebApp() {
var err error
// 2) inicializuj šifrovací službu
_, err = encrypt.NewService("")
if err != nil {
log.Fatal(err)
}
// 2) šablony
tmpl = template.Must(template.ParseGlob("templates/*.html"))
muxServer := NewServer()
log.Fatal(http.ListenAndServe(":8080", muxServer))
}