From d496ab89d560ea59f19669ea47ba9f991f7d8a94 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Fri, 4 Jun 2021 12:29:16 -0700 Subject: added activity sign and verify with pem keys --- main.go | 2 +- verification.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index aa0764c..a4adc33 100644 --- a/main.go +++ b/main.go @@ -2293,7 +2293,7 @@ func GetActorInstance(path string) (string, string) { if(len(match) > 2) { return match[2], match[3] } - } + } re = regexp.MustCompile(`(https?:\\)?(www)?([\w\d-_.:]+)\/([\w\d-_.]+)`) httpFormat := re.MatchString(path) diff --git a/verification.go b/verification.go index 99f5fb7..8c6cf98 100644 --- a/verification.go +++ b/verification.go @@ -8,10 +8,15 @@ import "time" import "os/exec" import "os" import "math/rand" +import "crypto" import "crypto/rsa" import "crypto/x509" +import "crypto/sha256" import "encoding/pem" +import "encoding/base64" import crand "crypto/rand" +import "io/ioutil" +import "strings" type Verify struct { Type string @@ -540,3 +545,48 @@ func StorePemToDB(db *sql.DB, actor Actor) { _, err = db.Exec(query, publicKeyPem, actor.Id, file) CheckError(err, "error creating publicKeyPem for actor ") } + +func ActivitySign(db *sql.DB, actor Actor, signature string) string { + query := `select file from publicKeyPem where id=$1 ` + + rows, err := db.Query(query, actor.PublicKey.Id) + + CheckError(err, "there was error geting actors public key id") + + var file string + defer rows.Close() + rows.Next() + rows.Scan(&file) + + file = strings.ReplaceAll(file, "public.pem", "private.pem") + _, err = os.Stat(file) + if err == nil { + publickey, err:= ioutil.ReadFile(file) + CheckError(err, "error reading file") + + block, _ := pem.Decode(publickey) + + pub, _ := x509.ParsePKCS1PrivateKey(block.Bytes) + rng :=crand.Reader + hashed := sha256.New() + hashed.Write([]byte(signature)) + cipher, _ := rsa.SignPKCS1v15(rng, pub, crypto.SHA256, hashed.Sum(nil)) + + return base64.StdEncoding.EncodeToString(cipher) + } + + return "" +} + +func ActivityVerify(db *sql.DB, actor Actor, signature string, verify string) error { + + sig, _ := base64.StdEncoding.DecodeString(signature) + + block, _ := pem.Decode([]byte(actor.PublicKey.PublicKeyPem)) + pub, _ := x509.ParsePKIXPublicKey(block.Bytes) + + hashed := sha256.New() + hashed.Write([]byte(verify)) + + return rsa.VerifyPKCS1v15(pub.(*rsa.PublicKey), crypto.SHA256, hashed.Sum(nil), sig) +} -- cgit v1.2.3