1 Star 1 Fork 0

Hyperledger Fabric 国密 / fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
issuer.go 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
Jtyoui 提交于 2021-07-22 15:59 . 国密
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package bridge
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-amcl/amcl"
"gitee.com/hyperledger-fabric-gm/fabric/bccsp"
"gitee.com/hyperledger-fabric-gm/fabric/bccsp/idemix/handlers"
cryptolib "gitee.com/hyperledger-fabric-gm/fabric/idemix"
"github.com/pkg/errors"
)
// IssuerPublicKey encapsulate an idemix issuer public key.
type IssuerPublicKey struct {
PK *cryptolib.IssuerPublicKey
}
func (o *IssuerPublicKey) Bytes() ([]byte, error) {
return proto.Marshal(o.PK)
}
func (o *IssuerPublicKey) Hash() []byte {
return o.PK.Hash
}
// IssuerPublicKey encapsulate an idemix issuer secret key.
type IssuerSecretKey struct {
SK *cryptolib.IssuerKey
}
func (o *IssuerSecretKey) Bytes() ([]byte, error) {
return proto.Marshal(o.SK)
}
func (o *IssuerSecretKey) Public() handlers.IssuerPublicKey {
return &IssuerPublicKey{o.SK.Ipk}
}
// Issuer encapsulates the idemix algorithms to generate issuer key-pairs
type Issuer struct {
NewRand func() *amcl.RAND
}
// NewKey generates a new issuer key-pair
func (i *Issuer) NewKey(attributeNames []string) (res handlers.IssuerSecretKey, err error) {
defer func() {
if r := recover(); r != nil {
res = nil
err = errors.Errorf("failure [%s]", r)
}
}()
sk, err := cryptolib.NewIssuerKey(attributeNames, i.NewRand())
if err != nil {
return
}
res = &IssuerSecretKey{SK: sk}
return
}
func (*Issuer) NewPublicKeyFromBytes(raw []byte, attributes []string) (res handlers.IssuerPublicKey, err error) {
defer func() {
if r := recover(); r != nil {
res = nil
err = errors.Errorf("failure [%s]", r)
}
}()
ipk := new(cryptolib.IssuerPublicKey)
err = proto.Unmarshal(raw, ipk)
if err != nil {
return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{
Type: bccsp.IdemixIssuerPublicKeyImporterUnmarshallingError,
ErrorMsg: "failed to unmarshal issuer public key",
Cause: err})
}
err = ipk.SetHash()
if err != nil {
return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{
Type: bccsp.IdemixIssuerPublicKeyImporterHashError,
ErrorMsg: "setting the hash of the issuer public key failed",
Cause: err})
}
err = ipk.Check()
if err != nil {
return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{
Type: bccsp.IdemixIssuerPublicKeyImporterValidationError,
ErrorMsg: "invalid issuer public key",
Cause: err})
}
if len(attributes) != 0 {
// Check the attributes
if len(attributes) != len(ipk.AttributeNames) {
return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{
Type: bccsp.IdemixIssuerPublicKeyImporterNumAttributesError,
ErrorMsg: fmt.Sprintf("invalid number of attributes, expected [%d], got [%d]",
len(ipk.AttributeNames), len(attributes)),
})
}
for i, attr := range attributes {
if ipk.AttributeNames[i] != attr {
return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{
Type: bccsp.IdemixIssuerPublicKeyImporterAttributeNameError,
ErrorMsg: fmt.Sprintf("invalid attribute name at position [%d]", i),
})
}
}
}
res = &IssuerPublicKey{PK: ipk}
return
}
Go
1
https://gitee.com/hyperledger-fabric-gm/fabric.git
git@gitee.com:hyperledger-fabric-gm/fabric.git
hyperledger-fabric-gm
fabric
fabric
v1.4.9

搜索帮助