1 Star 0 Fork 0

13683679291/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
message.go 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package entities
import (
"encoding/json"
"github.com/pkg/errors"
)
// SignedMessage is a simple struct that contains space
// for a payload and a signature over it, and convenience
// functions to sign, verify, marshal and unmarshal
type SignedMessage struct {
// ID contains a description of the entity signing this message
ID []byte `json:"id"`
// Payload contains the message that is signed
Payload []byte `json:"payload"`
// Sig contains a signature over ID and Payload
Sig []byte `json:"sig"`
}
// Sign signs the SignedMessage and stores the signature in the Sig field
func (m *SignedMessage) Sign(signer Signer) error {
if signer == nil {
return errors.New("nil signer")
}
m.Sig = nil
bytes, err := json.Marshal(m)
if err != nil {
return errors.Wrap(err, "sign error: json.Marshal returned")
}
sig, err := signer.Sign(bytes)
if err != nil {
return errors.WithMessage(err, "sign error: signer.Sign returned")
}
m.Sig = sig
return nil
}
// Verify verifies the signature over Payload stored in Sig
func (m *SignedMessage) Verify(verifier Signer) (bool, error) {
if verifier == nil {
return false, errors.New("nil verifier")
}
sig := m.Sig
m.Sig = nil
defer func() {
m.Sig = sig
}()
bytes, err := json.Marshal(m)
if err != nil {
return false, errors.Wrap(err, "sign error: json.Marshal returned")
}
return verifier.Verify(sig, bytes)
}
// ToBytes serializes the intance to bytes
func (m *SignedMessage) ToBytes() ([]byte, error) {
return json.Marshal(m)
}
// FromBytes populates the instance from the supplied byte array
func (m *SignedMessage) FromBytes(d []byte) error {
return json.Unmarshal(d, m)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mmcro/fabric.git
git@gitee.com:mmcro/fabric.git
mmcro
fabric
fabric
v1.3.0-rc1

搜索帮助

0d507c66 1850385 C8b1a773 1850385