代码拉取完成,页面将自动刷新
/*
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。