diff options
| author | Andrew <saintruler@gmail.com> | 2021-04-28 20:31:16 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2021-04-28 20:31:16 +0400 |
| commit | 8ebbdab5079f803567297af842c87ce012b7ea11 (patch) | |
| tree | ffe28a45797dab60b036eb05e419532cae329171 /http-client/cryptography.go | |
| parent | 7d6270f64b1dc00d91230b5c793bc49991f0fcf8 (diff) | |
Added cryptography functions and completed client and server.
Diffstat (limited to 'http-client/cryptography.go')
| -rw-r--r-- | http-client/cryptography.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/http-client/cryptography.go b/http-client/cryptography.go new file mode 100644 index 0000000..175e60b --- /dev/null +++ b/http-client/cryptography.go @@ -0,0 +1,60 @@ +package main + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/sha256" + "crypto/x509" + "encoding/asn1" + "encoding/base64" + "encoding/json" + "encoding/pem" + "fmt" +) + +const KeyBitSize = 4096 + +// Приватный ключ - отдаётся серверу +// Публичный ключ - сохраняется на клиенте + +func signRequest(req Request, key *rsa.PublicKey) (string, error) { + body, _ := json.Marshal(req) + based := base64.StdEncoding.EncodeToString(body) + h := sha256.Sum256([]byte(based)) + requestHash := fmt.Sprintf("%x", h) + + ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, key, []byte(requestHash)) + if err != nil { + return "", nil + } + + signature := base64.StdEncoding.EncodeToString(ciphertext) + return fmt.Sprintf("%s.%s", based, signature), nil +} + +func parseKey(keyBytes []byte) (*rsa.PublicKey, error) { + block, _ := pem.Decode(keyBytes) + return x509.ParsePKCS1PublicKey(block.Bytes) +} + +func generateKeys() (private []byte, public []byte, err error) { + key, err := rsa.GenerateKey(rand.Reader, KeyBitSize) + if err != nil { + return 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 bytePrivate, bytePublic, nil +} |