-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrsa.go
46 lines (43 loc) · 1.16 KB
/
rsa.go
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
package torgo
import (
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/asn1"
"encoding/base32"
"encoding/base64"
"errors"
"strings"
)
// OnionFromRSA returns an Onion instance from a 1024 bit RSA private key which
// can be used to start a hidden service with controller.AddOnion.
func OnionFromRSA(pri *rsa.PrivateKey) (*Onion, error) {
pub, ok := pri.Public().(*rsa.PublicKey)
if !ok {
return nil, errors.New("torgo: unable to extract *rsa.PublicKey")
}
serviceID, err := ServiceIDFromRSA(pub)
if err != nil {
return nil, err
}
der := x509.MarshalPKCS1PrivateKey(pri)
key := base64.StdEncoding.EncodeToString(der)
return &Onion{
Ports: make(map[int]string),
ServiceID: serviceID,
PrivateKey: key,
PrivateKeyType: "RSA1024",
}, nil
}
// ServiceIDFromRSA calculates a Tor service ID from an *rsa.PublicKey.
func ServiceIDFromRSA(pub *rsa.PublicKey) (string, error) {
der, err := asn1.Marshal(*pub)
if err != nil {
return "", err
}
// Onion id is base32(firstHalf(sha1(publicKeyDER)))
hash := sha1.Sum(der)
half := hash[:len(hash)/2]
serviceID := base32.StdEncoding.EncodeToString(half)
return strings.ToLower(serviceID), nil
}