Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/aliyundrive/help.go
1987 views
1
package aliyundrive
2
3
import (
4
"crypto/ecdsa"
5
"crypto/rand"
6
"encoding/hex"
7
"math/big"
8
9
"github.com/dustinxie/ecc"
10
)
11
12
func NewPrivateKey() (*ecdsa.PrivateKey, error) {
13
p256k1 := ecc.P256k1()
14
return ecdsa.GenerateKey(p256k1, rand.Reader)
15
}
16
17
func NewPrivateKeyFromHex(hex_ string) (*ecdsa.PrivateKey, error) {
18
data, err := hex.DecodeString(hex_)
19
if err != nil {
20
return nil, err
21
}
22
return NewPrivateKeyFromBytes(data), nil
23
24
}
25
26
func NewPrivateKeyFromBytes(priv []byte) *ecdsa.PrivateKey {
27
p256k1 := ecc.P256k1()
28
x, y := p256k1.ScalarBaseMult(priv)
29
return &ecdsa.PrivateKey{
30
PublicKey: ecdsa.PublicKey{
31
Curve: p256k1,
32
X: x,
33
Y: y,
34
},
35
D: new(big.Int).SetBytes(priv),
36
}
37
}
38
39
func PrivateKeyToHex(private *ecdsa.PrivateKey) string {
40
return hex.EncodeToString(PrivateKeyToBytes(private))
41
}
42
43
func PrivateKeyToBytes(private *ecdsa.PrivateKey) []byte {
44
return private.D.Bytes()
45
}
46
47
func PublicKeyToHex(public *ecdsa.PublicKey) string {
48
return hex.EncodeToString(PublicKeyToBytes(public))
49
}
50
51
func PublicKeyToBytes(public *ecdsa.PublicKey) []byte {
52
x := public.X.Bytes()
53
if len(x) < 32 {
54
for i := 0; i < 32-len(x); i++ {
55
x = append([]byte{0}, x...)
56
}
57
}
58
59
y := public.Y.Bytes()
60
if len(y) < 32 {
61
for i := 0; i < 32-len(y); i++ {
62
y = append([]byte{0}, y...)
63
}
64
}
65
return append(x, y...)
66
}
67
68