~welt/murse

ref: db85e43da90177a9ed7275eb7f2d6952407c1466 murse/crypto.go -rw-r--r-- 1.4 KiB
db85e43dwelt guiprep: adjust logging 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main

import (
	"crypto"
	"crypto/rsa"
	"crypto/sha512"
	"crypto/x509"
	"encoding/hex"
	"encoding/pem"
	"errors"
	"fmt"
	"log"
)

func init() {
	k, err := getKey()
	if err != nil {
		log.Fatal(err)
	}

	publicKey = k
}

func compareBytesHashSum(hash string, bytes *[]byte) (bool, error) {
	hdc, err := hex.DecodeString(hash)
	if err != nil {
		fmt.Println(err)
		return false, nil
	}

	var ha [48]byte
	copy(ha[:], hdc) // convert slice to array for hash checking
	hfb := sha512.Sum384(*bytes)

	if hfb == ha {
		return true, nil
	}
	return false, nil
}

func base16ToHash(hash string) (*[48]byte, error) {
	hdc, err := hex.DecodeString(hash)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}

	var ha [48]byte
	copy(ha[:], hdc) // convert slice to array

	return &ha, nil
}

func verifySig(sig *[]byte, bytes *[]byte) bool {
	hash := sha512.Sum384(*bytes)
	err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA384, hash[:], *sig)
	if err != nil {
		fmt.Println(err)
		return false
	}

	return true
}

func parseKey(b *[]byte) (*rsa.PublicKey, error) {
	pp /* heh */, _ := pem.Decode(*b)
	if pp == nil {
		return nil, errors.New("invalid key")
	}
	if pp.Type != "PUBLIC KEY" {
		return nil, errors.New("not a proper public key")
	}

	pk, err := x509.ParsePKIXPublicKey(pp.Bytes)
	if err != nil {
		return nil, errors.New("failed to parse key")
	}

	var pkt *rsa.PublicKey
	var ok bool
	if pkt, ok = pk.(*rsa.PublicKey); !ok {
		return nil, err
	}
	return pkt, nil
}