Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sm2中的P256Sm2方法返回值是elliptic.Curve,但该接口大部分方法已经是Deprecated状态 #214

Open
lenuse opened this issue Feb 5, 2025 · 0 comments

Comments

@lenuse
Copy link

lenuse commented Feb 5, 2025

当前go版本是1.23.4。

func HexToSM2PrivateKey(hexKey string) (*sm2.PrivateKey, error) {
	// 将 16 进制字符串转换为字节数组
	privateKeyBytes, err := hex.DecodeString(hexKey)
	if err != nil {
		return nil, fmt.Errorf("failed to decode hex: %w", err)
	}
	curve := sm2.P256Sm2()
	curve.Params()
	D := new(big.Int).SetBytes(privateKeyBytes)
	// 根据 D 生成公钥点 (x, y)
	x, y := curve.ScalarBaseMult(privateKeyBytes)

	// 生成 sm2 私钥结构
	privateKey := &sm2.PrivateKey{
		PublicKey: sm2.PublicKey{
			Curve: curve,
			X:     x,
			Y:     y,
		},
		D: D,
	}
	return privateKey, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant