-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility_cryptography.go
More file actions
45 lines (40 loc) · 1012 Bytes
/
Copy pathutility_cryptography.go
File metadata and controls
45 lines (40 loc) · 1012 Bytes
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
// ABOUTME: Cryptographic utilities for RSA keypair generation and
// ABOUTME: canonical public key encoding used to derive addresses.
package quark
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"errors"
)
func generateKeypair() (*rsa.PrivateKey, *rsa.PublicKey, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
return privateKey, &privateKey.PublicKey, nil
}
func marshalPublicKey(pub *rsa.PublicKey) ([]byte, error) {
return x509.MarshalPKIXPublicKey(pub)
}
func unmarshalPublicKey(b []byte) (*rsa.PublicKey, error) {
k, err := x509.ParsePKIXPublicKey(b)
if err != nil {
return nil, err
}
pub, ok := k.(*rsa.PublicKey)
if !ok {
return nil, errors.New("not an RSA public key")
}
return pub, nil
}
func addressFromPublicKey(pub *rsa.PublicKey) (string, error) {
b, err := marshalPublicKey(pub)
if err != nil {
return "", err
}
h := sha256.Sum256(b)
return hex.EncodeToString(h[:]), nil
}