1 Star 0 Fork 0

bigzoro/luyu_protocol

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
signature_data.go 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
张景涛 提交于 2024-05-23 17:12 +08:00 . 第一次提交
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
//}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xiaoshengdada/luyu_protocol.git
git@gitee.com:xiaoshengdada/luyu_protocol.git
xiaoshengdada
luyu_protocol
luyu_protocol
ebe377f84765

搜索帮助