aboutsummaryrefslogtreecommitdiff
path: root/verification.go
diff options
context:
space:
mode:
authorFChannel <>2021-06-04 12:29:16 -0700
committerFChannel <>2021-06-04 12:29:16 -0700
commitd496ab89d560ea59f19669ea47ba9f991f7d8a94 (patch)
tree75e037fbe536833f0f83009876f33b34db3d8266 /verification.go
parent41c63c0688475d5212ce2262b1be248bf438a9ad (diff)
added activity sign and verify with pem keys
Diffstat (limited to 'verification.go')
-rw-r--r--verification.go50
1 files changed, 50 insertions, 0 deletions
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)
+}