summaryrefslogtreecommitdiff
path: root/playground/cryptography.go
blob: 62206838bdb41c76bc46b12eba9c78bb479fb967 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/asn1"
	"encoding/base64"
	"encoding/pem"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

const KeyBitSize = 4096

func readFile(filename string) ([]byte, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}

	return ioutil.ReadAll(file)
}

func writeFile(filename string, data []byte) error {
	file, err := os.Create(filename)
	if err != nil {
		return err
	}

	_, err = file.Write(data)
	return err
}

func signData(data []byte, key *rsa.PublicKey) (string, error) {
	based := base64.StdEncoding.EncodeToString(data)
	h := sha256.Sum256([]byte(based))
	dataHash := fmt.Sprintf("%x", h)


	ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, key, []byte(dataHash))
	if err != nil {
		return "", err
	}

	signature := base64.StdEncoding.EncodeToString(ciphertext)
	return fmt.Sprintf("%s.%s", based, signature), nil
}

func checkSignature(signedData string, key *rsa.PrivateKey) (bool, error) {
	spl := strings.Split(signedData, ".")
	data := spl[0]
	signature := spl[1]

	dataHash := fmt.Sprintf("%x", sha256.Sum256([]byte(data)))

	decodedSign, err := base64.StdEncoding.DecodeString(signature)
	if err != nil {
		return false, err
	}

	signHash, err := decodeMessage(decodedSign, key)
	if err != nil {
		return false, err
	}

	return dataHash == string(signHash), nil
}

func encodeMessage(message []byte, key *rsa.PrivateKey) ([]byte, error) {
	ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, &key.PublicKey, message)
	if err != nil {
		return nil, err
	}
	return ciphertext, nil
}

func decodeMessage(ciphertext []byte, key *rsa.PrivateKey) ([]byte, error) {
	plaintext, err := rsa.DecryptPKCS1v15(rand.Reader, key, ciphertext)
	if err != nil { return nil, err }
	return plaintext, nil
}

func loadPublicPEM(filename string) (*rsa.PublicKey, error) {
	pemBytes, err := readFile(filename)
	if err != nil {
		return nil, err
	}

	block, _ := pem.Decode(pemBytes)
	if block == nil {
		return nil, errors.New("there is no key in file")
	}

	return x509.ParsePKCS1PublicKey(block.Bytes)
}

func loadPrivatePEM(filename string) (*rsa.PrivateKey, error) {
	pemBytes, err := readFile(filename)
	if err != nil {
		return nil, err
	}

	block, _ := pem.Decode(pemBytes)
	if block == nil {
		return nil, errors.New("there is no key in file")
	}

	return x509.ParsePKCS1PrivateKey(block.Bytes)
}

func generateKeys() (keys *rsa.PrivateKey, private []byte, public []byte, err error) {
	key, err := rsa.GenerateKey(rand.Reader, KeyBitSize)
	if err != nil {
		return nil, nil, nil, err
	}

	var privateKey = &pem.Block{
		Type: "PRIVATE KEY",
		Bytes: x509.MarshalPKCS1PrivateKey(key),
	}

	asn1Bytes, _ := asn1.Marshal(key.PublicKey)
	var publicKey = &pem.Block{
		Type: "PUBLIC KEY",
		Bytes: asn1Bytes,
	}

	bytePrivate := pem.EncodeToMemory(privateKey)
	bytePublic := pem.EncodeToMemory(publicKey)
	return key, bytePrivate, bytePublic, nil
}