代码拉取完成,页面将自动刷新
package sm2
import (
"fmt"
"math/big"
)
// SignatureData represents a signature consisting of the components v, r, and s.
type SignatureData struct {
V byte
R *big.Int
S *big.Int
}
const SM2PrivateKeySize = 32
// ParseSignatureData parses a signature from the provided bytes.
func ParseSignatureData(data []byte) (*SignatureData, error) {
if len(data) != 65 {
return nil, fmt.Errorf("illegal signature bytes length: %d %v", len(data), data)
}
r := new(big.Int).SetBytes(data[1 : 1+SM2PrivateKeySize])
s := new(big.Int).SetBytes(data[1+SM2PrivateKeySize : 1+SM2PrivateKeySize*2])
return &SignatureData{V: data[0], R: r, S: s}, nil
}
// ToBytes converts the signature to bytes.
func (sig *SignatureData) ToBytes() []byte {
result := make([]byte, SM2PrivateKeySize*2+1)
result[0] = sig.V
rBytes := sig.R.Bytes()
leadingZeros := SM2PrivateKeySize - len(rBytes)
copy(result[1+leadingZeros:], rBytes)
sBytes := sig.S.Bytes()
leadingZeros = SM2PrivateKeySize - len(sBytes)
copy(result[1+SM2PrivateKeySize+leadingZeros:], sBytes)
return result
}
// IsCanonical checks if the signature is canonical.
func (sig *SignatureData) IsCanonical() bool {
return sig.S.Cmp(HalfCurveOrder) <= 0
}
// ToCanonicalised adjusts the S component to be less than or equal to half the curve order if necessary.
//func (sig *SignatureData) ToCanonicalised() *SignatureData {
// if !sig.IsCanonical() {
// s := new(big.Int).Sub(CurveN, sig.S)
// return &SignatureData{V: sig.V, R: sig.R, S: s}
// }
// return sig
//}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。