63 Star 181 Fork 3

Gitee 极速下载 / hyperledger-fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
user.go 2.31 KB
一键复制 编辑 原始数据 按行查看 历史
Angelo De Caro 提交于 2018-11-05 13:45 . [FAB-9527] Use Idemix-Based BCCSP
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package handlers
import (
"crypto/sha256"
"github.com/hyperledger/fabric/bccsp"
"github.com/pkg/errors"
)
// userSecretKey contains the User secret key
type userSecretKey struct {
// sk is the idemix reference to the User key
sk Big
// Exportable if true, sk can be exported via the Bytes function
exportable bool
}
func NewUserSecretKey(sk Big, exportable bool) *userSecretKey {
return &userSecretKey{sk: sk, exportable: exportable}
}
func (k *userSecretKey) Bytes() ([]byte, error) {
if k.exportable {
return k.sk.Bytes()
}
return nil, errors.New("not exportable")
}
func (k *userSecretKey) SKI() []byte {
raw, err := k.sk.Bytes()
if err != nil {
return nil
}
hash := sha256.New()
hash.Write(raw)
return hash.Sum(nil)
}
func (*userSecretKey) Symmetric() bool {
return true
}
func (*userSecretKey) Private() bool {
return true
}
func (k *userSecretKey) PublicKey() (bccsp.Key, error) {
return nil, errors.New("cannot call this method on a symmetric key")
}
type UserKeyGen struct {
// Exportable is a flag to allow an issuer secret key to be marked as Exportable.
// If a secret key is marked as Exportable, its Bytes method will return the key's byte representation.
Exportable bool
// User implements the underlying cryptographic algorithms
User User
}
func (g *UserKeyGen) KeyGen(opts bccsp.KeyGenOpts) (bccsp.Key, error) {
sk, err := g.User.NewKey()
if err != nil {
return nil, err
}
return &userSecretKey{exportable: g.Exportable, sk: sk}, nil
}
// UserKeyImporter import user keys
type UserKeyImporter struct {
// Exportable is a flag to allow a secret key to be marked as Exportable.
// If a secret key is marked as Exportable, its Bytes method will return the key's byte representation.
Exportable bool
// User implements the underlying cryptographic algorithms
User User
}
func (i *UserKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
der, ok := raw.([]byte)
if !ok {
return nil, errors.New("invalid raw, expected byte array")
}
if len(der) == 0 {
return nil, errors.New("invalid raw, it must not be nil")
}
sk, err := i.User.NewKeyFromBytes(raw.([]byte))
if err != nil {
return nil, err
}
return &userSecretKey{exportable: i.Exportable, sk: sk}, nil
}
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v2.1.1

搜索帮助