Skip to content

Commit

Permalink
encrypt messages before sending them
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocolacio committed Dec 8, 2018
1 parent 7cd0a5d commit 7409afb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
22 changes: 12 additions & 10 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import(
"fmt"
"log"
"database/sql"
_ "github.com/go-sql-driver/mysql"
Expand All @@ -13,14 +14,15 @@ var (
func init() {
source := fmt.Sprintf("%s:%s@/%s", "root", "", "quicksilver")

db, err := sql.Open("mysql", source)
var err error
db, err = sql.Open("mysql", source)
if err != nil {
log.Fatal(err)
}
}

func InitTables() error {
_, err := db.Exec(`CREATE TABLE keys(
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS dh(
id INT PRIMARY KEY AUTO_INCREMENT,
owner VARCHAR(16),
pub BLOB,
Expand All @@ -31,36 +33,36 @@ func InitTables() error {
}

func ResetTables() error {
_, err := db.Exec("DROP TABLE IF EXISTS keys")
_, err := db.Exec("DROP TABLE IF EXISTS dh")
if err != nil {
return err
}

return InitTables()
}

func LookupPubKey(owner string) (pub []bytes, err error) {
func LookupPubKey(owner string) (pub []byte, err error) {
row := db.QueryRow(
`SELECT priv
FROM keys
FROM dh
WHERE owner = ?
ORDER BY id DESC`,
string, pub)
owner)
err = row.Scan(&pub)
return
}

func LookupPrivKey(owner string, pub []bytes) (priv []bytes, err error) {
func LookupPrivKey(owner string, pub []byte) (priv []byte, err error) {
row := db.QueryRow(
`SELECT priv
FROM keys
FROM dh
WHERE owner = ? AND pub = ?`,
string, pub)
owner, pub)
err = row.Scan(&priv)
return
}

func UploadKey(owner int, pub, priv []bytes) error {
func UploadKey(owner int, pub, priv []byte) error {
_, err := db.Exec(`INSERT INTO keys (owner, pub, priv) VALUES (?, ?, ?)`, owner, pub, priv)
return err
}
51 changes: 43 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package main

import(
// "fmt"
// "github.com/fabiocolacio/quicksilver/crypto"
// "crypto/elliptic"
// "crypto/rand"
"os"
"github.com/fabiocolacio/quicksilver/crypto"
"crypto/elliptic"
"crypto/rand"
"math/big"
// "os"
"encoding/json"
"log"
"time"
"github.com/fabiocolacio/quicksilver/gui"
"github.com/fabiocolacio/quicksilver/db"
"github.com/fabiocolacio/quicksilver/api"
"github.com/gotk3/gotk3/gtk"
"github.com/gotk3/gotk3/glib"
)

func main() {
config := os.Getenv("HOME") + "/.config/quicksilver"
err := os.MkdirAll(config, 0666)
err := db.InitTables()
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -52,15 +54,48 @@ func main() {
log.Fatal(err)
}

var(
myPriv []byte
myX *big.Int
myY *big.Int
peerX *big.Int
peerY *big.Int
)

if peerKey, err := db.LookupPubKey(peer); err != nil {
log.Fatal(err)
} else {
peerX, peerY = elliptic.Unmarshal(crypto.Curve, peerKey)
if peerX == nil {
log.Fatal(err)
}

myPriv, myX, myY, err = elliptic.GenerateKey(crypto.Curve, rand.Reader)
if err != nil {
log.Fatal(err)
}
}


// priv, x, y, err := elliptic.GenerateKey(crypto.Curve, rand.Reader)
// if err != nil {
// log.Fatal(err)
// }

go MessagePoll(jwt, peer, ui)

ui.Callback = func(msg string) {
err := api.MessageSend(jwt, peer, msg)
c, err := crypto.EncryptMessage([]byte(msg), myPriv, myX, myY, peerX, peerY)
if err != nil {
log.Println(err)
}

payload, err := json.Marshal(c)
if err != nil {
log.Println(err)
}

err = api.MessageSend(jwt, peer, string(payload))
if err != nil {
log.Println(err)
}
Expand Down

0 comments on commit 7409afb

Please sign in to comment.