29 lines
519 B
Go
29 lines
519 B
Go
package security
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"errors"
|
|
)
|
|
|
|
var PrivateKey *ecdsa.PrivateKey
|
|
var PublicKey ecdsa.PublicKey
|
|
|
|
func InitECCKey() error {
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
PrivateKey = key
|
|
PublicKey = key.PublicKey
|
|
return nil
|
|
}
|
|
|
|
func GetKeys() (*ecdsa.PrivateKey, *ecdsa.PublicKey, error) {
|
|
if PrivateKey == nil {
|
|
return nil, nil, errors.New("ECC私钥尚未初始化")
|
|
}
|
|
return PrivateKey, &PublicKey, nil
|
|
}
|